String get_path = "Request URL";
Handler Handler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
Mybean Mybean = (Mybean) msg.obj;
};
};
New Thread () {
public void Run () {
try {
Get the URL object, set the path to access
URL url = new URL (get_path);
Get networked operation class, HttpURLConnection
HttpURLConnection openconnection = (httpurlconnection) URL
. OpenConnection ();
Set the connection timeout, in milliseconds
Openconnection.setconnecttimeout (5000);
Set the request mode with Get and post
Openconnection.setrequestmethod ("GET");
Sets the read time-out, in milliseconds
Openconnection.setreadtimeout (5000);
Start connection
Openconnection.connect ();
Determine if the connection is successful through the response code
if (openconnection.getresponsecode () = = 200) {
Gets the data returned by the server, which is returned in the form of a stream
InputStream InputStream = OpenConnection
. getInputStream ();
BufferedReader BufferedReader = new BufferedReader (
New InputStreamReader (InputStream));
Accept the data inside the stream
String ReadData;
Stitching the data in the stream to ensure that the data is complete
StringBuilder builder = new StringBuilder ();
Use the while loop to read the data in the stream and jump out of the loop when the data in the stream is read
while ((ReadData = Bufferedreader.readline ()) = null) {
Builder.append (ReadData);
}
String stringdata = builder.tostring ();
Gson Gson = new Gson ();
Mybean Mybean = Gson.fromjson (StringData, Mybean.class);
Because the Android child thread cannot update the UI, it sends the data to the main thread processing
Message message = Message.obtain ();
Message.obj = Mybean;
Handler.sendmessage (message);
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
};
}.start ();
*************************************************************************************************************** ***
Httputils httputils = new Httputils ();
Httputils.send (Httpmethod.post, "", params,
New Requestcallback<string> () {
@Override
public void OnFailure (HttpException arg0, String arg1) {
TODO auto-generated Method Stub
}
@Override
public void onsuccess (responseinfo<string> arg0) {
TODO auto-generated Method Stub
string string = Arg0.result;
}
});
HttpURLConnection Request Network | | Httputils Request Network