The first type:
public static String Invoke () {
String result = null;
try {
Final String url = "http://192.168.1.104:180/";
HttpPost HttpPost = new HttpPost (URL);
Defaulthttpclient httpClient = new Defaulthttpclient ();
Basic Identity Verification
Basiccredentialsprovider bcp = new Basiccredentialsprovider ();
String userName = "Liudong";
String Password = "123";
Bcp.setcredentials (Authscope.any, New Usernamepasswordcredentials (
UserName, password));
Httpclient.setcredentialsprovider (BCP);
HttpResponse HttpResponse = Httpclient.execute (HttpPost);
StringBuilder builder = new StringBuilder ();
BufferedReader reader = new BufferedReader (New InputStreamReader (
Httpresponse.getentity (). getcontent ());
for (String s = reader.readline (); s! = null; s = Reader.readline ()) {
Builder.append (s);
}
result = Builder.tostring ();
LOG.D (TAG, "result is (" + result + ")");
} catch (Exception e) {
LOG.E (TAG, e.tostring ());
}
LOG.D (TAG, "over");
return result;
}
The second type:
public static string SendRequest (String adress_http, String Strjson) {
String returnline = "";
try {
System.out.println ("************** Start HTTP communication **************");
SYSTEM.OUT.PRINTLN ("The interface address of the ************** call is **************" + adress_http);
SYSTEM.OUT.PRINTLN ("The data sent by ************** request is **************" + Strjson);
URL my_url = new URL (adress_http);
HttpURLConnection connection = (httpurlconnection) my_url.openconnection ();
Connection.setdooutput (TRUE);
Connection.setdoinput (TRUE);
Connection.setrequestmethod ("POST");
Connection.setusecaches (FALSE);
Connection.setinstancefollowredirects (TRUE);
Connection.setrequestproperty ("Content-type", "Application/json");
Connection.connect ();
DataOutputStream out = new DataOutputStream (connection
. Getoutputstream ());
byte[] content = strjson.getbytes ("Utf-8");
Out.write (content, 0, content.length);
Out.flush ();
Out.close (); Flush and close
BufferedReader reader = new BufferedReader (New InputStreamReader (Connection.getinputstream (), "Utf-8"));
StringBuilder builder = new StringBuilder ();
String line = "";
System.out.println ("Contents of Post request Start");
while (line = Reader.readline ()) = null) {
line = new String (Line.getbytes (), "utf-8");
Returnline + = line;
System.out.println (line);
}
System.out.println ("Contents of Post Request Ends");
Reader.close ();
Connection.disconnect ();
SYSTEM.OUT.PRINTLN ("======== Returns the result of ========" + returnline);
} catch (Exception e) {
E.printstacktrace ();
}
return returnline;
}
The third type:
Protected Daoreturnobject doinbackground (Jsonobject ... jsonobjects) {
Daoreturnobject Returnobject;
try {
Publishprogress ("In process ...");
String ServerURL = "http://130.251.10.195:8091";//mserversettinginfodao.getinstance (). GETURL ();
String url = serverurl+ "/customerlogin";
HttpPost HttpPost = new HttpPost (URL);
LOG.I ("url", url);
Bytearrayentity arrayentity = null;
byte[] Jsonbyte = null;
try {
Jsonbyte = Jsonobjects[0].tostring (). GetBytes (default_encoding);
arrayentity = new Bytearrayentity (jsonbyte);
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
throw new Exception ("Data packaging Error!");
}
LOG.D ("M-client", "Request json:\n" + new String (Jsonbyte, default_encoding));
Httppost.setentity (arrayentity);
Httppost.setheader ("Accept", "Application/json");
Httppost.setheader ("Content-type", "application/x-www-form-urlencoded");
HttpResponse HttpResponse;
Byte[] Responsebyte;
String Responsestr;
try {
Defaulthttpclient httpclient = new Defaulthttpclient ();
Set cookies
HttpContext localcontext = new Basichttpcontext ();
HttpResponse = Httpclient.execute (HttpPost, Localcontext);
if (Httpresponse.getstatusline (). Getstatuscode ()! = 200) {
throw new Exception ("Error receiving data:" + httpresponse.getstatusline (). toString ());
}
Responsebyte = Entityutils.tobytearray (Httpresponse.getentity ());
Write Cache
Mserversettinginfodao.getinstance (). Setstreaminfo (Mclientfunction.getfiledir (),
Responsebyte.length, "res");
Mclientfunction.setrescurrentstream (responsebyte.length);
LOG.D ("M-client", "Response json:\n" + New String (responsebyte, 0, Responsebyte.length, default_encoding));
Responsestr = new String (responsebyte, 0, Responsebyte.length, default_encoding);
} catch (Clientprotocolexception e) {
LOG.E ("M-client", "Error receiving data!", e);
throw new Exception ("Error receiving data:" + e.getmessage (), E);
} catch (IOException e) {
LOG.E ("M-client", "Error receiving data!", e);
throw new Exception ("Error receiving data:" + e.getmessage (), E);
}
try {
map<string, object> Responsemap = (map<string, object>) jsonutil.json2object (new JSONObject (RESPONSESTR)) ;
Returnobject = new Daoreturnobject (Integer.parseint (String) responsemap.get ("Code"), (String) responsemap.get ("msg "), Responsemap.get (" res "));
} catch (Jsonexception e) {
LOG.E ("M-client", "Error receiving data!", e);
throw new Exception ("Error receiving data:" + e.getmessage (), E);
}
} catch (Exception e) {
return new Daoreturnobject (E.getmessage (), NULL);
}
return returnobject;
}
Remember to add access rights: <uses-permission android:name= "Android.permission.INTERNET"/>
Android HTTP JSON request 3 different ways