AsycHttpClient框架

1.用android自带的api实现http的get请求

    public void get(final Handler handler, final String webUrl){
        //用android自带的api实现http请求
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url=new URL(webUrl);
                    HttpURLConnection connection= (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(60000);
                    connection.setReadTimeout(60000);
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    InputStream inputStream=connection.getInputStream();
                    byte[] data=new byte[1024];
                    int count=0;
                    StringBuffer stringBuffer=new StringBuffer();
                    while ((count=inputStream.read(data))>0){
                        stringBuffer.append(new String(data,0,count));
                    }
                    Message message=handler.obtainMessage();
                    Bundle bundle=new Bundle();
                    bundle.putString("data",stringBuffer.toString());
                    message.setData(bundle);
                    handler.sendMessage(message);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

  handler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                Bundle bundle=msg.getData();
                String data=bundle.getString("data");
               Log.d(TAG, data);
            }
        };
2.使用AsycHttpClient

 AsyncHttpClient client=new AsyncHttpClient();
        client.get(url, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, org.apache.http.Header[] headers, byte[] bytes) {
                Log.d(TAG,"get:"+ new String(bytes));
            }

            @Override
            public void onFailure(int i, org.apache.http.Header[] headers, byte[] bytes, Throwable throwable) {
                Log.d(TAG,"failure");
            }
        });

        RequestParams params=new RequestParams();
        params.add("username","gst");
        params.add("pwd","123");
        client.post(url, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Log.d(TAG,"post:"+ new String(bytes));
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Log.d(TAG,"failure");
            }
        });


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值