android,HttpPost 提交数据


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

});

posted @ 2012-11-07 10:44  bert.zeng  阅读(272)  评论(0编辑  收藏  举报