HttpClient_Post请求

本文介绍了一个简单的Android应用如何通过HTTP Post请求从服务器获取新闻数据的过程。应用使用了DefaultHttpClient发起请求,并通过传递参数的方式定制请求内容,最终解析返回的JSON数据。

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

    /**
     * 点击事件...post方式获取
     * @param view
     */
    public void getData(View view){

        //通过线程的方式来获取数据
        getDataByThread();

    }

    /**
     * 通过线程的方式来获取数据
     */
    private void getDataByThread() {
        new Thread(){
            @Override
            public void run() {
                try {
                    //1.有一个请求的http客户端对象
                    HttpClient client = new DefaultHttpClient();

                    String path = "http://v.juhe.cn/toutiao/index";
                    //2.指定请求方式的对象
                    HttpPost httpPost = new HttpPost(path);

                    //5.创建传递参数的集合....并且把传递的参数放到集合中
                    List<BasicNameValuePair> params = new ArrayList<>();

                    params.add(new BasicNameValuePair("type","top"));
                    params.add(new BasicNameValuePair("key","597b4f9dcb50e051fd725a9ec54d6653"));

                    //4.创建一个请求实体内容的对象,,,,UrlEncodedFormEntity支持url编码,并且支持form格式
                    //list<? extends NameValuePair> params 要给服务器传递的参数,,,所有的参数需要放到集合里面,string encoding指定编码字符集

                    HttpEntity entity = new UrlEncodedFormEntity(params,"utf-8");
                    //3.http协议中,post请求方式,请求的参数是在请求的实体内容中....setEntity设置请求实体内容的对象
                    httpPost.setEntity(entity);

                    //6.执行post请求
                    HttpResponse httpResponse = client.execute(httpPost);

                    //7.获取
                    int statusCode = httpResponse.getStatusLine().getStatusCode();
                    if (statusCode == 200){
                        //获取到响应的字节流
                        InputStream inputStream = httpResponse.getEntity().getContent();

                        String json = streamToString(inputStream,"utf-8");

                        Log.i("--json---",json);
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    private String streamToString(InputStream inputStream,String charset) {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String s = null;
            StringBuilder builder = new StringBuilder();
            while ((s = bufferedReader.readLine()) != null){
                builder.append(s);
            }

            bufferedReader.close();
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  null;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值