Getting Data From the Web (URLConnection via http)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby greval » Fri Feb 08, 2008 7:25 pm

Hey,

I'm getting as answer HTTP 403 - Forbidden :(
Any idea?
I am behind firewall, but I have wrote the item for the proxy in the adb shell and I can go online with the browser. Is a firewall problem?

Thanks!
greval
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 05, 2008 12:46 pm

Top

Postby plusminus » Fri Feb 08, 2008 8:05 pm

Hello greval,

did you try to fetch any other URI instead of what the one within the tutorial :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby greval » Fri Feb 08, 2008 11:28 pm

Yes, also the wheather one or one page with gnu license in txt file (this could be to long?), but every time I got the same error. :roll:
What do you think?
Thanks
greval
Freshman
Freshman
 
Posts: 4
Joined: Tue Feb 05, 2008 12:46 pm

Postby plusminus » Sat Feb 09, 2008 11:51 am

Hello greval,

sorry no clue about proxy-issues :(, because I'm not behind one.
I think the next SDK-update could (not sure!!!) improve this issue. Google is promising big improvements on the ADT-Tool. (Last update they added the feature that you can simulate incoming sms/call from the ADT).

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby cs_cckai » Wed Sep 24, 2008 11:06 am

Hi,

I am using eclipse version 3.4
android SDK 0.9Beta.

Actually, I have try your code, but it throw to exception with unknow error, I found that when it run
InputStream is = ucon.getInputStream();
then throw exception.

Can anyone tell me why?
cs_cckai
Freshman
Freshman
 
Posts: 5
Joined: Wed Sep 24, 2008 8:32 am

Postby blackpearl » Thu Oct 09, 2008 8:08 am

cs_cckai wrote:Hi,

Actually, I have try your code, but it throw to exception with unknow error, I found that when it run
InputStream is = ucon.getInputStream();
then throw exception.

Can anyone tell me why?


Did you Specify Permission in AndroidManifest file? In the new version, permission needs to be granted.
Please add the following line in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />

http://code.google.com/android/devel/security.html
blackpearl
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Oct 09, 2008 8:01 am

Top

Postby JavaAndroid » Sat Oct 18, 2008 1:33 pm

Hi All,
I tried this tutorial, but its not working for me. Here is my Class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import java.io.BufferedInputStream;
  2. import java.io.InputStream;
  3. import java.net.URL;
  4. import java.net.URLConnection;
  5.  
  6. import org.apache.http.util.ByteArrayBuffer;
  7.  
  8. import android.app.Activity;
  9. import android.os.Bundle;
  10. import android.widget.TextView;
  11.  
  12. public class WebDataActivity extends Activity {
  13.     /** Called when the activity is first created. */
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.        
  18.         TextView textView = new TextView(this);
  19.         String myString = null;
  20.         try{
  21.                 URL url = new URL("http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt");
  22.                
  23.                 URLConnection urlConn = url.openConnection();
  24.                
  25.                 InputStream is = urlConn.getInputStream();
  26.                 BufferedInputStream bis = new BufferedInputStream(is);
  27.                
  28.                 ByteArrayBuffer baf = new ByteArrayBuffer(50);
  29.                 int current = 0;
  30.                 while((current = bis.read()) != -1){
  31.                 baf.append((byte)current);
  32.            }
  33.                
  34.                 myString = new String(baf.toByteArray());
  35.  
  36.         }catch(Exception e){
  37.                 myString = e.getMessage();
  38.                
  39.         }
  40.         textView.setText(myString);
  41.         this.setContentView(textView);
  42.     }
  43. }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


When i run the Application, i m getting Unknown Error in the screen.
Code: Select all
this is the error which i m getting in the console
10-18 17:39:32.646: ERROR/mountd(19): could not read initial mass storage state
10-18 17:39:33.356: ERROR/flash_image(28): can't find recovery partition
10-18 17:39:57.517: ERROR/MemoryHeapBase(52): error opening /dev/pmem: No such file or directory
10-18 17:39:57.546: ERROR/SurfaceFlinger(52): Couldn't open /sys/android_power/wait_for_fb_sleep or /sys/android_power/wait_for_fb_wake
10-18 17:39:57.626: ERROR/GLLogger(52): couldn't load <libhgl.so> library (Cannot find library)
10-18 17:39:57.896: ERROR/GLLogger(52): couldn't load <libhgl.so> library (Cannot find library)
10-18 17:40:06.157: ERROR/BatteryService(52): Could not open '/sys/class/power_supply/usb/online'
10-18 17:40:06.166: ERROR/BatteryService(52): Could not open '/sys/class/power_supply/battery/batt_vol'
10-18 17:40:06.178: ERROR/BatteryService(52): Could not open '/sys/class/power_supply/battery/batt_temp'
10-18 17:40:06.697: ERROR/EventHub(52): could not get driver version for /dev/input/mouse0, Not a typewriter
10-18 17:40:06.726: ERROR/EventHub(52): could not get driver version for /dev/input/mice, Not a typewriter
10-18 17:40:06.876: ERROR/System(52): Failure starting core service
10-18 17:40:06.876: ERROR/System(52): java.lang.SecurityException
10-18 17:40:06.876: ERROR/System(52):     at android.os.BinderProxy.transact(Native Method)
10-18 17:40:06.876: ERROR/System(52):     at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
10-18 17:40:06.876: ERROR/System(52):     at android.os.ServiceManager.addService(ServiceManager.java:72)
10-18 17:40:06.876: ERROR/System(52):     at com.android.server.ServerThread.run(SystemServer.java:155)
10-18 17:40:06.906: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
10-18 17:40:07.806: ERROR/SystemServer(52): Failure starting StatusBarService
10-18 17:40:07.806: ERROR/SystemServer(52): java.lang.NullPointerException
10-18 17:40:07.806: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.updateBluetooth(StatusBarPolicy.java:749)
10-18 17:40:07.806: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.<init>(StatusBarPolicy.java:282)
10-18 17:40:07.806: ERROR/SystemServer(52):     at com.android.server.status.StatusBarPolicy.installIcons(StatusBarPolicy.java:337)
10-18 17:40:07.806: ERROR/SystemServer(52):     at com.android.server.ServerThread.run(SystemServer.java:186)
10-18 17:40:07.839: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
10-18 17:40:08.497: ERROR/GpsLocationProvider(52): Could not open GPS configuration file /etc/gps.conf
10-18 17:40:08.497: ERROR/GpsLocationProvider(52): java.io.FileNotFoundException: /etc/gps.conf
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:227)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at java.io.FileInputStream.<init>(FileInputStream.java:76)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at com.android.internal.location.GpsLocationProvider.<init>(GpsLocationProvider.java:179)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at com.android.server.LocationManagerService._loadProvidersNoSync(LocationManagerService.java:356)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at com.android.server.LocationManagerService.loadProvidersNoSync(LocationManagerService.java:340)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at com.android.server.LocationManagerService.loadProviders(LocationManagerService.java:333)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at com.android.server.LocationManagerService.<init>(LocationManagerService.java:447)
10-18 17:40:08.497: ERROR/GpsLocationProvider(52):     at com.android.server.ServerThread.run(SystemServer.java:230)
10-18 17:40:08.526: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
10-18 17:40:33.957: ERROR/AndroidRuntime(126): ERROR: thread attach failed
10-18 17:40:26.184: ERROR/AndroidRuntime(134): ERROR: thread attach failed
10-18 17:40:27.212: ERROR/NetworkStateTracker(52): Can't set tcp buffer sizes:java.io.FileNotFoundException: /sys/kernel/ipv4/tcp_rmem_min
10-18 17:40:38.293: ERROR/OSNetworkSystem(191): unknown socket error -1
10-18 17:40:55.384: ERROR/NetworkStateTracker(52): Can't set tcp buffer sizes:java.io.FileNotFoundException: /sys/kernel/ipv4/tcp_rmem_min

I m absolutely clueless :shock:

Can anybody help me out?

Thanks in Advance

Thanks
JavaAndroid
JavaAndroid
Freshman
Freshman
 
Posts: 7
Joined: Wed Oct 15, 2008 8:02 am

Postby bino » Sun Oct 19, 2008 5:26 am

@JavaAndroid,
You can refer to blackpearl's previous post.
It will help, your manifest file must have the line <uses-permission android:name="android.permission.INTERNET" /> so that you can access to
the internet.
Like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your package's name"
android:versionCode="1"
android:versionName="1.0.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WebDataActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
User avatar
bino
Freshman
Freshman
 
Posts: 9
Joined: Mon Oct 13, 2008 7:15 am

Postby zachok » Sun Dec 07, 2008 2:03 am

How would I go about just getting a piece of text out of a website?

say I just wanted the word "Powered" from the example. Or a currency conversion rate?

New poster, newish to java, can't wait to get my G1.
zachok
Freshman
Freshman
 
Posts: 2
Joined: Sun Dec 07, 2008 1:57 am

Http request and animation

Postby mikpg » Wed Dec 10, 2008 10:49 pm

I am having a problem animating child view rows of my list. When I animate the child view list I have a repopulation conflict with each other. I get a crash with a nullpointer exception in the getView method of my list adapter. Already tried repopulating the list in the onAnimationEnd method in an animation listener. When I kill the animation my http request works fine any suggestions would be greatly appreciated
Mikpg
mikpg
Freshman
Freshman
 
Posts: 5
Joined: Tue Nov 11, 2008 4:02 pm
Location: null

Postby deenybird » Fri Dec 12, 2008 8:41 pm

thanks guys so much for this helpful tutorial.
i have a quick question.
Could someone please explain what i would have to add/change to grab data from TWO places on the web.

The program would start, and display a txt file from 1 website, and below display a txt file from another.

thanks for pointing me in the right direction.
deenybird
Developer
Developer
 
Posts: 25
Joined: Sat Nov 29, 2008 12:35 am

Postby Timmeah » Tue Feb 10, 2009 4:49 pm

I'm getting an error when I try to run this application on my computer.

Does someone maybe have a clue what can be wrong. The error only says unknown error.
Attachments
dataweb.jpg
dataweb.jpg (21.85 KiB) Viewed 25418 times
Timmeah
Junior Developer
Junior Developer
 
Posts: 14
Joined: Tue Feb 10, 2009 4:30 pm

Postby OrdeS » Mon Feb 16, 2009 4:44 am

Timmeah wrote:I'm getting an error when I try to run this application on my computer.

Does someone maybe have a clue what can be wrong. The error only says unknown error.


This I'm sure is a error caused by a missing INTERNET permission on your app. Its set in the manifest xml. Check above on how to add it.
OrdeS
Freshman
Freshman
 
Posts: 3
Joined: Sat Feb 14, 2009 6:30 am

Postby alpesh_harsoda » Tue Feb 17, 2009 12:06 pm

Hi, add this permission to your manifest file.
<uses-permission android:name="android.permission.INTERNET" /> :)
Regards,
Alpesh Patel,
Logistic Infotech
http://www.logisticinfotech.com
User avatar
alpesh_harsoda
Senior Developer
Senior Developer
 
Posts: 106
Joined: Sat Sep 20, 2008 12:35 pm
Location: Rajkot,Gujarat(India)

Doesn't work with SSL

Postby heikki » Wed Feb 25, 2009 7:49 am

Has anyone tried that with SSL, like https://www.google.com

I just get:

Code: Select all
java.io.IOException: Hostname <www.google.com> was not verified
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.getSecureSocket(HttpConnection.java:169)
at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection$HttpsEngine.connect(HttpsURLConnection.java:398)
at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1030)
at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnection.getInputStream(HttpsURLConnection.java:252)
at net.heikkitoivonen.android.chandlerqe.ChandlerQE.onCreate(ChandlerQE.java:38)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1122)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2103)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2156)
at android.app.ActivityThread.access$1800(ActivityThread.java:112)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1580)
at android.os.Handler.dispatchMessage(Handler.java:88)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3742)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:497)
at dalvik.system.NativeStart.main(Native Method)
heikki
Freshman
Freshman
 
Posts: 8
Joined: Thu Feb 07, 2008 5:45 am

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: Google Feedfetcher and 5 guests