1. This is called through the JSON pass value
(1)Public static void Appadd () {
try {
//Create connection
url url = new URL (add_url);
httpurlconnection connection = (httpurlconnection) URL
. OpenConnection ();
Connection.setdooutput (true);
Connection.setdoinput (true);
Connection.setrequestmethod ("POST");
connection.setusecaches (false);
connection.setinstancefollowredirects (true);
connection.setrequestproperty ("Content-type",
"application/x-www-form-urlencoded");
Connection.connect ();
//post Request
DataOutputStream out = new DataOutputStream (
Connection.getoutputstream ());
Jsonobject obj = new Jsonobject ();
obj.element ("Mesconent", Java.net.URLEncoder.encode ("Zhan"));
obj.element ("Phonelist", "15662165280");
out.writebytes (obj.tostring ());
Out.flush ();
out.close ();
//Read response
BufferedReader reader = new BufferedReader (New InputStreamReader (
Connection.getinputstream ()));
String lines;
stringbuffer sb = new StringBuffer ("");
While ((lines = Reader.readline ()) = null) {
lines = new String (Lines.getbytes (), "Utf-8");
sb.append (lines);
}
System.out.println (SB);
reader.close ();
//Disconnect
Connection.disconnect ();
} catch (Malformedurlexception e) {
//TODO auto-generated catch block
e.printstacktrace ();
} catch (Unsupportedencodingexception e) {
//TODO auto-generated catch block
e.printstacktrace ();
} catch (IOException e) {
//TODO auto-generated catch block
e.printstacktrace ();
}
}
(2) Xmlinfo is a JSON character channeling
public static string Dohttppost (String xmlinfo,string URL) {
System.out.println ("Originating Data:" +xmlinfo);
InputStream instr = null;
Java.io.ByteArrayOutputStream out = null;
try{
URL url = new url (URL);
URLConnection Urlcon = Url.openconnection ();
Urlcon.setdooutput (TRUE);
Urlcon.setdoinput (TRUE);
Urlcon.setusecaches (FALSE);
Urlcon.setrequestproperty ("Content-type", "application/x-www-form-urlencoded");
DataOutputStream printout = new DataOutputStream (Urlcon.getoutputstream ());
Printout.writebytes (Xmlinfo);
Printout.flush ();
Printout.close ();
Read response
BufferedReader reader = new BufferedReader (New InputStreamReader (
Urlcon.getinputstream ()));
String lines;
StringBuffer sb = new StringBuffer ("");
while ((lines = Reader.readline ()) = null) {
lines = new String (Lines.getbytes (), "utf-8");
Sb.append (lines);
}
System.out.println (SB);
Reader.close ();
return sb.tostring ();
}catch (Exception e) {
E.printstacktrace ();
return "0";
}
finally {
try {
Out.close ();
Instr.close ();
}catch (Exception ex) {
return "0";
}
}
}
2 pass values such as URL value via parameters? A=? &b=?
Using Httpcliend
String url= "";
HttpClient client1=new HttpClient ();
Client1.setconnectiontimeout (30000);
Postmethod Postmethod = new Postmethod (URL);
Client1.getparams (). Setcontentcharset ("UTF-8");
Postmethod.setparameter ("Businesid", "1");
Postmethod.setparameter ("ItemId", "2");
try {
Client1.executemethod (Postmethod);
Jsonobject result_code= (Jsonobject) Jsonobject.parse (postmethod.getresponsebodyasstring ());
String result_id=result_code.getstring ("state");
String error_msg=result_code.getstring ("error");
if (Result_id.equals ("200")) {
}
} catch (HttpException e) {
TODO Auto-generated catch block
E.printstacktrace ();
} catch (IOException e) {
TODO Auto-generated catch block
E.printstacktrace ();
}
Attention to things
Is the code specification that appears when you submit a request
Application/x-www-form-urlencoded encode all characters before sending (default)
Multipart/form-data characters are not encoded. You must use this value when you use a form that contains a file upload control.
The Text/plain space is converted to the "+" plus sign, but does not encode special characters.
These are the most basic packages of httpclient calls.
About the HTTP request invocation interface