Skip to content

Instantly share code, notes, and snippets.

@SZooo
Created August 28, 2014 11:40
Show Gist options
  • Save SZooo/c054130721f3309ef411 to your computer and use it in GitHub Desktop.
Save SZooo/c054130721f3309ef411 to your computer and use it in GitHub Desktop.
android http协议post请求方式 (不完整示例)
private Button button1,button2,button3;
private TextView textView1;
button1.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//URLַ
// String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
String uriAPI = "http://172.20.0.206:8082//TestServelt/login.do";
/*建立HTTP Post连线*/
HttpPost httpRequest =new HttpPost(uriAPI);
//Post运作传送变数必须用NameValuePair[]阵列储存
//传参数 服务端获取的方法为request.getParameter("name")
List <NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name","this is post"));
try{
//发出HTTP request
httpRequest.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
//取得HTTP response
HttpResponse httpResponse=new DefaultHttpClient().execute(httpRequest);
//若状态码为200 ok
if(httpResponse.getStatusLine().getStatusCode()==200){
//取出回应字串
String strResult=EntityUtils.toString(httpResponse.getEntity());
textView1.setText(strResult);
}else{
textView1.setText("Error Response"+httpResponse.getStatusLine().toString());
}
}catch(ClientProtocolException e){
textView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
textView1.setText(e.getMessage().toString());
e.printStackTrace();
} catch (IOException e) {
textView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment