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 ());
}
}
}