Android sends json and parses the returned json

Source: Internet
Author: User

Package com. http. test;


Import org. apache. http. HttpResponse;
Import org. apache. http. HttpStatus;
Import org. apache. http. client. HttpClient;
Import org. apache. http. client. methods. HttpGet;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. apache. http. util. EntityUtils;
Import org. json. JSONException;
Import org. json. JSONObject;
Import org. json. JSONTokener;


Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
// Import android. widget. EditText;
Import android. widget. TextView;


Public class Http_testActivity extends Activity {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );


Final TextView TV = (TextView) findViewById (R. id. result );
// Final EditText ed = (EditText) findViewById (R. id. sendurl );
Button bt = (Button) findViewById (R. id. send );


Bt. setOnClickListener (new OnClickListener () {// create the first click event


Public void onClick (View v ){

String strResult = null;


Try {
String httpUrl = "http: // 10.10.10.10: 61002/userMessage/cJobConsultationUnread. json? Data = 688656 & client_id = 20012 & view_id = 268800 ";
// HttpGet connection object
HttpGet httpRequest = new HttpGet (httpUrl );
// Get the HttpClient object
HttpClient httpclient = new DefaultHttpClient ();
// Request HttpClient to obtain HttpResponse
HttpResponse httpResponse = httpclient.exe cute (httpRequest );
// The request is successful.
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
// Obtain the returned string
StrResult = EntityUtils. toString (httpResponse
. GetEntity ());
TV. setText (strResult );
} Else {
TV. setText ("request error! ");
}


} Catch (Exception e ){


}

// Returned json string strResult = {"status": 0, "message": "OK", "data": 15}
Try {
 
JSONTokener jsonParser = new JSONTokener (strResult );
JSONObject js = (JSONObject) jsonParser. nextValue ();
// The next step is the JSON object operation.
System. out. println ("status value:" + js. getString ("status "));
System. out. println ("message Value:" + js. getString ("message "));
System. out. println ("data value:" + js. getInt ("data "));

} Catch (JSONException ex ){
// Exception Handling Code
}


}


});


}
}

 


From mobile Internet performance, android automation, and web automation testing Column

Related Article

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.