public static string HttpPost (string URL, Jsonobject json) {
String respcontent = null;
try{
HttpPost HttpPost = new HttpPost (URL);
Closeablehttpclient client = Httpclients.createdefault ();
JSON mode
stringentity entity = new Stringentity (json.tostring (), "utf-8");//solve Chinese garbled problem
Entity.setcontentencoding ("UTF-8");
Entity.setcontenttype ("Application/json");
Httppost.setentity (entity);
System.out.println ();
HttpResponse resp = Client.execute (HttpPost);
if (Resp.getstatusline (). Getstatuscode () = = 200) {
Httpentity he = resp.getentity ();
Respcontent = entityutils.tostring (He, "UTF-8");
}
}catch (Exception ex) {
Todo:handle exception
Respcontent=null;
}finally {
return respcontent;
}
}
/**
* @param URL
* The address to be requested
* @return Status Code
* @throws IOException
* @throws clientprotocolexception
*/
public static string HttpGet (string url) {
String urlnamestring = URL;
String status = NULL;
try {
Get requests by address
HttpGet request = new HttpGet (urlnamestring);//Send GET requests here
Request.getparams (). Setparameter (httpmethodparams.so_timeout,3000);
Gets the current client object
HttpClient HttpClient = new Defaulthttpclient ();
Getting a response object from a Request object
HttpResponse response;
Response = Httpclient.execute (request);
Determine if the network connection status code is normal (0--200 are normal)
if (Response.getstatusline (). Getstatuscode () = = 200) {
Status = "200";
}
}
catch (Exception e) {
TODO auto-generated Catch block
E.printstacktrace ();
Log.error (e);
}
finally {
return status;
}
}
HttpClient Get/post Request