WEB开发网
开发学院手机开发Android 开发 Android 通过 http 协议 POST 传输方式 阅读

Android 通过 http 协议 POST 传输方式

 2010-04-04 06:06:00 来源:WEB开发网   
核心提示:Android通过http协议POST传输方式如下:方式一:HttpPost(import org.apache.http.client.methods.HttpPost)代码如下:private Button button1,button2,button3;private TextView textView1;but

Android通过http协议POST传输方式如下:

方式一:HttpPost(import org.apache.http.client.methods.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://172.20.0.206:8082//TestServelt/login.do";

/*建立HTTP Post连线*/

HttpPost httpRequest =new HttpPost(uriAPI);

//Post运作传送变数必须用NameValuePair[]阵列储存

//传参数 服务端获取的方法为request.getParameter("name")

List params=new ArrayList();

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

1 2 3  下一页

Tags:Android 通过 http

编辑录入:coldstar [复制链接] [打 印]
赞助商链接