http connection problem - response code 401

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

http connection problem - response code 401

Postby ghostxz » Thu Oct 29, 2009 2:13 am

Hi guys. i want to acces a jpg image from my ip camera, and i don't know how to implement the login part. in browser it is simple - insert user name and password and you are in. here is the code, working for non-passworded urls:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void onCreate(Bundle savedInstanceState) {
  2.  
  3.         super.onCreate(savedInstanceState);
  4.  
  5.         TextView tv=new TextView(this);
  6.  
  7.         int response=-1;
  8.  
  9.                 try {
  10.  
  11.                         URL url = new URL("http://<valid_image_url>");
  12.  
  13.                
  14.  
  15.                 URLConnection conn = url.openConnection();
  16.  
  17.                 if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection.");
  18.  
  19.                 HttpURLConnection httpConn = (HttpURLConnection) conn;
  20.  
  21.                 httpConn.setAllowUserInteraction(false);
  22.  
  23.                 httpConn.setConnectTimeout(1000);
  24.  
  25.                 httpConn.setReadTimeout(1000);
  26.  
  27.                 httpConn.setInstanceFollowRedirects(true);
  28.  
  29.                
  30.  
  31.                 httpConn.setDoOutput(true);
  32.  
  33.             httpConn.setDoInput(true);
  34.  
  35.             httpConn.setRequestMethod("POST");
  36.  
  37.            
  38.  
  39.                 httpConn.setRequestMethod("GET");
  40.  
  41.                 httpConn.connect();
  42.  
  43.                 response = httpConn.getResponseCode();
  44.  
  45.                 if (response == HttpURLConnection.HTTP_OK) {
  46.  
  47.                         InputStream in = httpConn.getInputStream();
  48.  
  49.                         Bitmap bitmap = BitmapFactory.decodeStream(in);
  50.  
  51.                         tv.setText("HTTP_OK");
  52.  
  53.                 }
  54.  
  55.                 else {
  56.  
  57.                         tv.setText(String.valueOf(response));
  58.  
  59.                 }
  60.  
  61.                 } catch (Exception e) {
  62.  
  63.                         // TODO Auto-generated catch block
  64.  
  65.                         tv.setText(e.getMessage().toString());
  66.  
  67.                         e.printStackTrace();
  68.  
  69.                 }
  70.  
  71.                 setContentView(tv);
  72.  
  73.     }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I tried to replace the URL with "http://username:passwor@<camera_ip>/jpg/image.jpg" but i have got exception.
thank you for any help
ghostxz
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 29, 2009 1:56 am

Top

Postby RS » Thu Oct 29, 2009 12:45 pm

Hello ghostxz!

Probably you are having problems with the authentication, check this: http://www.anddev.org/parsing_xml_from_a_site_protected_with_basic_http_auth-t8588.html

Regards,
RS
Code hard, go pro! Don't wait for someone to do your own project.
User avatar
RS
Junior Developer
Junior Developer
 
Posts: 11
Joined: Tue Oct 20, 2009 10:49 pm

Postby ghostxz » Thu Oct 29, 2009 3:52 pm

I solved my problem with this:
..
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bitmap bitmap = null;
  2.  
  3. DefaultHttpClient client = new DefaultHttpClient();
  4.  
  5. client.getCredentialsProvider().setCredentials(new AuthScope(null, -1),new UsernamePasswordCredentials("user", "password"));           
  6.  
  7. HttpGet request = new HttpGet("http://192.168.0.103/snapshot.jpg");                                            
  8.  
  9. try {
  10.  
  11.         HttpResponse resp = client.execute(request);
  12.  
  13.         bitmap = BitmapFactory.decodeStream(resp.getEntity().getContent());
  14.  
  15. } catch (Exception e) {
  16.  
  17.         e.printStackTrace();
  18.  
  19. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4

..
It works Ok on the emulator, but not on my hero. I don't know why, I've got an IO Exception:

Code: Select all
10-29 16:28:52.550: ERROR/PackageInstallationReceiver(339): Remove /data/local/tmp/com.sandu.SMS.apk Fail!

10-29 16:28:52.560: WARN/System.err(339): java.io.IOException: Error running exec(). Commands: [/system/xbin/su, 0, /system/bin/rm, /data/local/tmp/com.sandu.ARC.apk] Working Directory: null Environment: null


how can I resolve this, without rooting my phone? Adding "android.permission.USE_CREDENTIALS" doesn't help anymore.
ghostxz
Freshman
Freshman
 
Posts: 2
Joined: Thu Oct 29, 2009 1:56 am

Postby padde » Thu Oct 29, 2009 5:26 pm

http://192.168.0.103/snapshot.jpg looks like a Intranet IP.. with your phone you cant connect to that adress.. use a picture from the internet to test your app.
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 7 guests