URL to resolve network requests in Android

Source: Internet
Author: User

Recently doing Android Web application development, using the knowledge of Android network request, now to introduce the knowledge of network request, we know that Android sends a request to the server, (this is what we usually call the POST request), We want to send a complete URL, and then the server side to receive the URL, the URL for a specific resolution, is to parse the URL, converted to JSON data, and then we just have to process the JSON data.


I now use my project instance to reflect the process of parsing the URL:

1, the process of assembling the URL:

Private String Getorderpayurl (int order, int action, String accountid,string token) {Calendar calendar = Calendar.getinsta NCE (); Long time = Calendar.gettimeinmillis ()/1000;return Orderpayurl + "? action=" + Action + "&time=" + time+ "& Accountid= "+ AccountId +" &token= "+ token +" &paymoney= "+ Order +" ¤cy=cny& "+" sign= "+ getsign (action, time , accountid);}

2, the process of sending the URL:

private void HttpRequest (string url, int which, string method) {Httprequesttask task = new Httprequesttask (Mhandler, URL, which, method); Task.starttask ();}

Where Mhandler is a defined local variable and uses the handler type to handle the returned parse result,

public class Httprequesttask implements Runnable {private Handler handler;private String url;private int which;private Str ing method;public httprequesttask (Handler Handler, string url, int which, string method) {This.url = Url;this.handler = Ha Ndler;this.which = Which;this.method = method;} public void StartTask () {new Thread (this). Start (); @Overridepublic void Run () {looper.prepare (); SendRequest ();} private void SendRequest () {String result = Null;if (Method! = null && method.equals (myconstant.post)) {result = H Ttputil.querystringforpost (URL);} if (Method! = null && method.equals (myconstant.get)) {result = Httputil.querystringforget (URL);} LOG.E ("---result---", result); Message msg = Message.obtain (); msg.what = Which;msg.obj = Result;handler.sendmessage (msg);}}


3, the process of parsing the URL:

Send a POST request to get a response to the query result public static string querystringforpost (string url) {HttpPost request = httputil.gethttppost (URL); String result = null;try {//Gets the response object HttpResponse response = httputil.gethttpresponse (request); if (response.getstatusline (). Getstatuscode () = = () {result = Entityutils.tostring (response.getentity ()); return result;}} catch (Clientprotocolexception e) {e.printstacktrace (); result = "Network exception!" "; return result;} catch (IOException e) {e.printstacktrace (); result = "Network exception!" "; return result;} return result;}

Get POST Request object Requestpublic static HttpPost gethttppost (String URL) {//Remove space//if (URL! = null) {//pattern p = pattern.co Mpile ("\\s");//matcher m = p.matcher (URL);//url = M.replaceall ("");//}httppost request = new HttpPost (URL); return Request;}

Which of the package files we want to use is

Org.apache.http.client.methods.HttpPost
Actually the result string returned is a JSON-type string, we just need to use jsonobject to process the corresponding JSON, we need to get the data, return, OK,

This is actually a clearer process, which can also be seen in multithreaded processing patterns.

Once we need the network request, we usually put the processing part of the network request in the sub-thread, another thread, so that the original thread does not handle too many things, which also reduces the main thread of the pressure.

URL to resolve network requests in Android

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.