Android HttpURLConnection connecting error only on hardware devices (not in emulators) -


i'm new in android developing , looking way download pictures , text internet app. found few tutorials , included code in project. when i'm running app on emulator work fine. if run app on device (samsung galaxy s4) "connecting error" (in httpdownload.java) , have no idea why, cause in other apps internet working.

thank u help.

here codes:

httpdownload.java:

import android.app.activity; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.os.bundle;  import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.httpurlconnection; import java.net.url; import java.net.urlconnection;  public class httpdownload extends activity {      /** called when activity first created. */     @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);     }      private inputstream openhttpconnection(string urlstring)             throws ioexception     {         inputstream in = null;         int response = -1;          url url = new url(urlstring);         urlconnection conn = url.openconnection();          if (!(conn instanceof httpurlconnection))             throw new ioexception("not http connection");          try{             httpurlconnection httpconn = (httpurlconnection) conn;             httpconn.setallowuserinteraction(false);             httpconn.setinstancefollowredirects(true);             httpconn.setrequestmethod("get");             httpconn.connect();              response = httpconn.getresponsecode();             if (response == httpurlconnection.http_ok) {                 in = httpconn.getinputstream();             }         }         catch (exception ex)         {             throw new ioexception("error connecting");         }         return in;     }      public bitmap downloadimage(string url)     {         bitmap bitmap = null;         inputstream in = null;         try {             in = openhttpconnection(url);             bitmap = bitmapfactory.decodestream(in);             in.close();         } catch (ioexception e1) {             // todo auto-generated catch block             e1.printstacktrace();         }         return bitmap;     }      public string downloadtext(string url)     {         int buffer_size = 2000;         inputstream in = null;         try {             in = openhttpconnection(url);         } catch (ioexception e1) {             // todo auto-generated catch block             e1.printstacktrace();             return "";         }          inputstreamreader isr = new inputstreamreader(in);         int charread;         string str = "";         char[] inputbuffer = new char[buffer_size];         try {             while ((charread = isr.read(inputbuffer))>0)             {                 //---convert chars string---                 string readstring =                         string.copyvalueof(inputbuffer, 0, charread);                 str += readstring;                 inputbuffer = new char[buffer_size];             }             in.close();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();             return "";         }         return str;     }  } 

main_activity:

import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.imageview; import android.widget.textview;   public class mainactivity extends actionbaractivity {      textview meintext;     imageview meinbild;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          meinbild = (imageview)findviewbyid(r.id.meinbild);         meintext = (textview)findviewbyid(r.id.meintext);     }      public void laden (view view) {         httpdownload download = new httpdownload();         meinbild.setimagebitmap(download.downloadimage("http://www.queness.com/resources/images/png/apple_ex.png"));     }  } 

logcat:

    local branch:     remote branch:     local patches:     reconstruct branch: 04-29 16:36:28.757  32038-32038/com.example.itsme.internetdaten d/openglrenderer﹕ enabling debug mode 0 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ java.io.ioexception: error connecting 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ com.example.itsme.internetdaten.httpdownload.openhttpconnection(httpdownload.java:50) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ com.example.itsme.internetdaten.httpdownload.downloadimage(httpdownload.java:60) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ com.example.itsme.internetdaten.mainactivity.laden(mainactivity.java:29) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ java.lang.reflect.method.invokenative(native method) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:515) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.view.view$1.onclick(view.java:3964) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.view.view.performclick(view.java:4633) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.view.view$performclick.run(view.java:19330) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.os.handler.handlecallback(handler.java:733) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.os.handler.dispatchmessage(handler.java:95) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.os.looper.loop(looper.java:157) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ android.app.activitythread.main(activitythread.java:5356) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ java.lang.reflect.method.invokenative(native method) 04-29 16:36:53.862  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ java.lang.reflect.method.invoke(method.java:515) 04-29 16:36:53.882  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1265) 04-29 16:36:53.882  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1081) 04-29 16:36:53.882  32038-32038/com.example.itsme.internetdaten w/system.err﹕ @ dalvik.system.nativestart.main(native method) 04-29 16:36:53.882  32038-32038/com.example.itsme.internetdaten i/choreographer﹕ skipped 61 frames!  application may doing work on main thread. 

manifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"     package="com.example.itsme.internetdaten" >      <uses-permission android:name="android.permission.internet" />      <application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme" >         <activity             android:name=".mainactivity"             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> 

change :

  catch (exception ex)         {              throw new ioexception("error connecting");         }  

to

catch (exception ex)         {             log.d("error", ex.tostring());         }  

and share exact exception occurring

also download image internet can use:

 url url = new url(address);  object content = url.getcontent(); 

thirdly, can might not have active internet connection throwing exception.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -