加载网络的url工具的两个方法(直接使用)

 

(一)、使用网络异步的方法:

调用代码:

 new AsyncTask<String, Void, String>() {
                    @Override
                    protected String doInBackground(String... strings) {
                        String httpUrl = strings[0];
                        return getHttpUrlConnection(httpUrl);
                    }

                    @Override
                    protected void onPostExecute(String s) {
                        super.onPostExecute(s);
                        Log.e("tag", "responseJson :" + s);
                        if (s == null) {
                            return;
                        }

                        parserJson(s);

                    }
                }.execute(mHttpUrl);


 

方法:

    //加载网络路径
    /**
     * 网络异步获取数据
     *
     * @param httpUrl
     * @return
     */
    public String getHttpUrlConnection(String httpUrl) {
        HttpURLConnection httpURLConnection = null;
        InputStream inputStream = null;
        try {
            URL url = new URL(httpUrl);
            httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setConnectTimeout(500);
            httpURLConnection.setDoInput(true);

            if (httpURLConnection.getResponseCode() == 200) {
                Log.e("tag","123123");
                inputStream = httpURLConnection.getInputStream();
                String responseJson = readInPutStream(inputStream);
                Log.e("tag","123"+responseJson);
                return responseJson;
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
            }
        }
        return null;
    }
    //读取流
    public String readInPutStream(InputStream inputStream) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line = "";
        StringBuilder stringBuilder = new StringBuilder();
        try {
            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }

 


(二)、使用第三方打包好的库:

需要下载两个库导入项目。

http://download.csdn.net/detail/zhangli_/9389498

http://download.csdn.net/detail/zhangli_/9389506

 

调用代码:

AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
        asyncHttpClient.get(url, new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, String s, Throwable throwable) {
                Log.e("tag", "onFailure  AsysncHttpClicent :" + s);
            }

            @Override
            public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, String s) {
                Log.e("tag", "onSuccess  AsysncHttpClicent :" + s);
                parserJson(s);

            }
        });

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值