Android uses Post to access dynamic web and Android post dynamic web

Source: Internet
Author: User

Android uses Post to access dynamic web and Android post dynamic web

Public class MyPostWebDemo extends Activity {
Private static final String PATH = "http://gouservice.juecheng.cn/Actv/Rich? Token = 7766721ce641cd6e58721a785942e8f7 ";
Private TextView info;

// Private boolean flag;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
// TODO Auto-generated method stub
Super. onCreate (savedInstanceState );
// Cause of android. OS. NetworkOnMainThreadException Error
// Cause: network access in the main thread is not allowed
// The solution can also be implemented asynchronously.
StrictMode. setThreadPolicy (new StrictMode. ThreadPolicy. Builder ()
. DetectDiskReads (). detectDiskWrites (). detectNetwork ()
. PenaltyLog (). build ());
StrictMode. setVmPolicy (new StrictMode. VmPolicy. Builder ()
. DetectLeakedSqlLiteObjects (). detectLeakedClosableObjects ()
. PenaltyLog (). penaltyDeath (). build ());
Super. setContentView (R. layout. main_post );
This.info = (TextView) super. findViewById (R.id.info );

// New Thread (){
// @ Override
// Public void run (){
//
//}
//};

Try {
// Submission path
HttpPost request = new HttpPost (PATH );
// Set the submission Parameters
List <NameValuePair> params = new ArrayList <NameValuePair> ();
Params. add (new BasicNameValuePair ("Friends", "42 "));
Params. add (new BasicNameValuePair ("PageNo", "1 "));
Params. add (new BasicNameValuePair ("CountPerPage", "10 "));
Params. add (new BasicNameValuePair ("LatestZanCount", "10 "));
Params. add (new BasicNameValuePair ("LatestCommentCount", "1 "));
// Set the Encoding
Request. setEntity (new UrlEncodedFormEntity (params, HTTP. UTF_8 ));
// Receive response
HttpResponse response = new defaulthttpclient(cmd.exe cute (request );

// String d = EntityUtils. toString (response. getEntity (). trim ();
// Info. setText (d );
If (response. getStatusLine (). getStatusCode ()! = 404 ){
String d = (EntityUtils. toString (
// Receive returned information
Response. getEntity ()));
Info. setText (d );
}

} Catch (Exception e ){
E. printStackTrace ();
Info. setText (e. toString ());
}

}

}

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.