Unable to connect to HTTP server and fetch json string in android -


i trying fetch json string, url: http://ip-api.com/json. got io exception while try fetch json string.

package www.howdy.com;  import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.list;  import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.util.entityutils; import org.json.jsonexception; import org.json.jsonobject;  import android.util.log;    public class serverhandler {       static inputstream = null;     static jsonobject jobj = null;     static string json = "";      //this method returns json object.     public static jsonobject makehttprequest(string url){     //http client helps send , receive data     defaulthttpclient httpclient = new defaulthttpclient();     //our request method post     httppost httppost = new httppost(url);     try {     //get response     httpresponse httpresponse = httpclient.execute(httppost);     httpentity httpentity = httpresponse.getentity();     // content , store inputstream object.     = httpentity.getcontent();     } catch (clientprotocolexception e) {         log.d("error++++++++++++++++++++++++++++", "client protocol exception");     // todo auto-generated catch block     e.printstacktrace();     } catch (ioexception e) {     // todo auto-generated catch block     e.printstacktrace();     log.d("error++++++++++++++++++++++++++++", "io exception ");     }     try {     //convert byte-stream character-stream.     bufferedreader reader = new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8);     stringbuilder sb = new stringbuilder();     string line = null;     try {     while((line = reader.readline())!=null){     sb.append(line+"\n");     }     //close input stream     is.close();     json = sb.tostring();     try {     jobj = new jsonobject(json);     } catch (jsonexception e) {     // todo auto-generated catch block         log.d("error++++++++++++++++++++++++++++", "json exception");     e.printstacktrace();     }     } catch (ioexception e) {     // todo auto-generated catch block     e.printstacktrace();     log.d("error++++++++++++++++++++++++++++", "io 2 exception");     }     } catch (unsupportedencodingexception e) {     // todo auto-generated catch block     e.printstacktrace();     log.d("error++++++++++++++++++++++++++++", "unsupportedencodingexception e");     }     return jobj;     } } 

and main activity using asynchronous task,

private class start extends asynctask<void, void, void> {              @override             protected void onpreexecute() {                 super.onpreexecute();                  txt.settext("");              }                protected void doinbackground(void... arg0){                  //                 j = serverhandler.makehttprequest(url);                  try {                     ab = j.getstring("as");                 } catch (jsonexception e) {                     // todo auto-generated catch block                     toast.maketext(getapplicationcontext(), "json exception", toast.length_short).show();                     e.printstacktrace();                  }                 return null;                 }              @override             protected void onpostexecute(void result) {                 super.onpostexecute(result);                 txt.settext(ab);             }           } 

where j jsonobject, ab string , txt textview. whenever run code, got io exception in logcat.

log.d("error++++++++++++++++++++++++++++", "io exception "); 


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -