网络请求(HttpUrlConnection/HttpClient)

httpurlconnection

get方式请求

            URL url=new URL(params[0]);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            if(connection.getResponseCode()==200){
                InputStream is=connection.getInputstream();
                //调用自定义的json解析的工具类
                String json=jiesonutils.getjson(is);
                return 
            }

post

    URL url=new URL("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm");
                    HttpURLConnection connection=(HttpURLConnection) url.openConnection();

                    connection.setRequestMethod("POST");
                    //设置参数
                    connection.setDoOutput(true);//是否向服务器写参数
                    StringBuffer sb=new StringBuffer();
                    sb.append("tel=").append(et.getText().toString());
                    //写参数,写字节数组(吧参数内容转为字节数组)
                    byte[] be=sb.toString().getBytes();
                    connection.getOutputStream().write(be);

                    if(connection.getResponseCode()==200){
                        InputStream is=connection.getInputStream();
                        byte[] b=new byte[1024];
                        ByteArrayOutputStream bo=new ByteArrayOutputStream();
                        int length=0;
                        while((length=is.read(b))!=-1){
                            bo.write(b, 0, length);
                        }
                        String st=bo.toString("gbk");
                        String string=st.substring(st.indexOf("{"));
                        System.out.println(string);
                        jiexi(string);
                    }

HttpClient

get

//客户端请求
            HttpClient client=new DefaultHttpClient();
            //请求方式
            HttpGet get=new HttpGet(params[0]);
            //执行get请求,获得相应数据
            HttpResponse response=client.execute(get);
            if(response.getStatusLine().getStatusCode()==200){
                InputStream is=response.getEntity().getContent();
                //tojsonutils自定义的解析json的工具类
                return tojsonutils.getjson(is);
            }

Post

    //定义一个请求客户端对象
                    HttpClient client=new DefaultHttpClient();
                    //定义post请求
                    HttpPost httpPost=new HttpPost(path);
                    //创建存放键值对的集合
                    List<BasicNameValuePair> parameters=new ArrayList<BasicNameValuePair>() ;
                    parameters.add(new BasicNameValuePair("info", et.getText().toString()));
                    parameters.add(new BasicNameValuePair("key", "65e2651ed3d6c03fb7172df8059578b8"));
                    //创建一个通过url编码过的表单对象
                    HttpEntity entity=new UrlEncodedFormEntity(parameters, "utf-8");
                    //设置请求的实体
                    httpPost.setEntity(entity);
                    //执行post请求获取对象
                    HttpResponse response = client.execute(httpPost);
                    if(response.getStatusLine().getStatusCode()==200){
                        InputStream is=response.getEntity().getContent();
                        String data=httputils.parseStream(is);
                        System.out.println(data);
                        Gson gson=new Gson();
                        bean be=gson.fromJson(data, bean.class);
                        list.add(be.result);

                        for(Result1 r:list){
                            str=r.text;
                            System.out.println("!!!!!!!!!!!!!!"+str);
                        }
                        runOnUiThread(new Runnable() {

                            @Override
                            public void run() {
                                // TODO Auto-generated method stub
                                tv.setText(str);
                            }
                        });
                    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值