HttpURLConnection Request Network | | Httputils Request Network

Source: Internet
Author: User
Tags response code

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.