工具类 获取网络数据的方法

来源:互联网 发布:宋仲基五官知乎 编辑:程序博客网 时间:2024/05/22 22:17

 注意: JsonCallBack   通过接口把json格式的字符串传递回去....

           public interface JsonCallBack {


           public void getJsonString(String json);


    }



public static void getData(final String path, Context context, final JsonCallBack callBack){



        if (NetWorkUtil.isConn(context)){


            AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() {
                @Override
                protected String doInBackground(Void... voids) {


                    try {
                        URL url = new URL(path);


                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setRequestMethod("GET");
                        connection.setReadTimeout(5000);
                        connection.setConnectTimeout(5000);


                        int responseCode = connection.getResponseCode();
                        if (responseCode == 200){
                            InputStream inputStream = connection.getInputStream();


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


                            return json;
                        }


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


                    return null;
                }


                @Override
                protected void onPostExecute(String s) {
                    //通过接口把json格式的字符串传递回去....
                    callBack.getJsonString(s);
                }
            };


            asyncTask.execute();


        }else {
            NetWorkUtil.showNoNetWorkDlg(context);
        }


    }