HttpClient的psot方式

HttpClient的post方式:


package com.example.dugaolong.httpurlconnectiondemo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dugaolong on 16/5/16.
 */
public class HttpClientPostTest extends Activity {
    private static final String TAG = Postactivity.class.getSimpleName();

    private String postUrl = "http://172.16.150.82:8080/http/servlet/Http";
    private Handler mHandler;
    private TextView tv;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_httpclient_post);

        button = (Button) findViewById(R.id.bt_post);
        tv = (TextView) findViewById(R.id.tv_message);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                doPost();
            }
        });
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if(msg.what == 1){
                    Bundle bundle = msg.getData();
                    String res = bundle.getString("res");
//                    tv.setText(msg.obj.toString());
                    tv.setText(res);
                }
            }
        };
    }

    /**
     * 执行post方法
     */
    private void doPost() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpPost httpPost = new HttpPost(postUrl);
                HttpClient httpClient = new DefaultHttpClient();
                try {
                    //创建参数列表
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("sex","girl"));
                    //url格式编码
                    UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params,"UTF-8");
                    httpPost.setEntity(urlEncodedFormEntity);
                    //执行请求
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    //请求结果
                    if (httpResponse.getStatusLine().getStatusCode()==200){
                        String result = EntityUtils.toString(httpResponse.getEntity());
                        Message msg = new Message();
//                        msg.obj=result;
                        msg.what = 1;
                        Bundle bundle = new Bundle();
                        bundle.putString("res",result);
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);
                        Log.i(TAG,"result:"+result);
                    }

                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
        }).start();
    }


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值