android - How to make Sync or Async HTTP Post/Get -


i need sync or async http post/get html data web-service. search whole internet can't give result.

i tried use examples:

but nothing of them working me.

httpclient , httpget cross out, error :

"org.apache.http.client.httpclient deprecated "

code:

try {     httpclient client = new defaulthttpclient();     string geturl = "google.com";     httpget = new httpget(geturl);     httpresponse responseget = client.execute(get);     httpentity resentityget = responseget.getentity();     if (resentityget != null)     {         //do response      } } catch (exception e) {     e.printstacktrace(); } 

the example have posted below based on example found on android developer docs. can find example here, @ more comprehensive example.

you able make http requests following

import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.widget.toast;  import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.reader; import java.io.unsupportedencodingexception; import java.net.httpurlconnection; import java.net.url;  public class mainactivity extends activity {     private static final string tag = mainactivity.class.getsimplename();      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          new downloadtask().execute("http://www.google.com/");     }      private class downloadtask extends asynctask<string, void, string> {          @override         protected string doinbackground(string... params) {             //do request in here don't interrupt ui thread             try {                 return downloadcontent(params[0]);             } catch (ioexception e) {                 return "unable retrieve data. url may invalid.";             }         }          @override         protected void onpostexecute(string result) {             //here done task             toast.maketext(mainactivity.this, result, toast.length_long).show();         }     }      private string downloadcontent(string myurl) throws ioexception {         inputstream = null;         int length = 500;          try {             url url = new url(myurl);             httpurlconnection conn = (httpurlconnection) url.openconnection();             conn.setreadtimeout(10000 /* milliseconds */);             conn.setconnecttimeout(15000 /* milliseconds */);             conn.setrequestmethod("get");             conn.setdoinput(true);             conn.connect();             int response = conn.getresponsecode();             log.d(tag, "the response is: " + response);             = conn.getinputstream();              // convert inputstream string             string contentasstring = convertinputstreamtostring(is, length);             return contentasstring;         } {             if (is != null) {                 is.close();             }         }     }      public string convertinputstreamtostring(inputstream stream, int length) throws ioexception, unsupportedencodingexception {         reader reader = null;         reader = new inputstreamreader(stream, "utf-8");         char[] buffer = new char[length];         reader.read(buffer);         return new string(buffer);     } } 

you can play around code suit needs


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -