四种网络数据请求汇总

来源:互联网 发布:js判断是不是标点符号 编辑:程序博客网 时间:2024/05/05 15:15
import java.io.BufferedReader;


//网络请求数据有多种方式,以下汇总了4种请求方式,供方便调用 Add by AL
//调用方式如下:
// RequestNetDataUtils.requestDataByHttpGet("http://data.tvguo.tv/mobile/commCnf?millis=1" ,new RequestDataInterface() {
//@Override
//public void getNetData(String data) {
//  LogUtil.e("anl", "data=======================" + data);
//}
//});
// RequestNetDataUtils.requestDataByHttpPost("http://data.tvguo.tv/mobile/commCnf","millis=1" ,new RequestDataInterface() {
//@Override
//public void getNetData(String data) {
//  LogUtil.e("anl", "data=======================" + data);
//}
//});
//RequestNetDataUtils.requestDataByHttpClientGet("http://data.tvguo.tv/mobile/commCnf?millis=1" ,new RequestDataInterface() {
//@Override
//public void getNetData(String data) {
//  LogUtil.e("anl", "data=======================" + data);
//}
//});
//RequestNetDataUtils.requestDataByHttpClientPost("http://data.tvguo.tv/mobile/commCnf", "millis=1" ,new RequestDataInterface() {
//@Override
//public void getNetData(String data) {
//  LogUtil.e("anl", "data=======================" + data);
//}
//});
public class RequestNetDataUtils {
    private final static String TAG = "RequestNetDataUtils";


    public interface RequestDataInterface {
        void getNetData(String data);
    }


    // 第一种使用HTTP的GET请求方式,传进去一个完整的url,返回一个String
    // 如:url为http://data.tvguo.tv/mobile/commCnf?millis=1
    public static void requestDataByHttpGet(String url, final RequestDataInterface dataInterface) {
        LogUtil.e(TAG, "url==============" + url);
        new AsyncTask<String, Void, Void>() {
            @Override
            protected Void doInBackground(String... params) {
                try {
                    StringBuffer requestData = new StringBuffer();
                    URL url = new URL(params[0]);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    if (connection.getResponseCode() == 200) {
                        InputStream inputStream = connection.getInputStream();
                        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                        String line;
                        while ((line = bufferedReader.readLine()) != null) {
                            requestData.append(line);
                        }
                        bufferedReader.close();
                        inputStreamReader.close();
                        inputStream.close();
                        LogUtil.e(TAG, "HttpGet==============" + requestData.toString());
                        dataInterface.getNetData(requestData.toString());
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(url);
    }


    // 第二种使用HTTP的POST请求方式,传一个完整的url和一个keyPath,返回一个String
    // 如url为http://data.tvguo.tv/mobile/commCnf;keyPath为“millis=1”
    public static void requestDataByHttpPost(String url, final String keyPath, final RequestDataInterface dataInterface) {
        LogUtil.e(TAG, "url==============" + url);
        LogUtil.e(TAG, "keyPath==============" + keyPath);
        new AsyncTask<String, Void, Void>() {
            @Override
            protected Void doInBackground(String... params) {
                try {
                    StringBuffer requestData = new StringBuffer();
                    URL url = new URL(params[0]);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setDoInput(true);
                    connection.setDoOutput(true);
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    connection.setRequestMethod("POST");
                    OutputStreamWriter osw = new OutputStreamWriter(connection.getOutputStream());
                    BufferedWriter bw = new BufferedWriter(osw);
                    bw.write(keyPath);
                    bw.flush();
                    InputStream inputStream = connection.getInputStream();
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
                    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                    String line;
                    while ((line = bufferedReader.readLine()) != null) {
                        requestData.append(line);
                    }
                    bufferedReader.close();
                    inputStreamReader.close();
                    inputStream.close();
                    LogUtil.e(TAG, "HttpPost==============" + requestData.toString());
                    dataInterface.getNetData(requestData.toString());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(url);
    }


    // 第三种方式是使用HttpClient进行Get方式通信
    // 如:url为http://data.tvguo.tv/mobile/commCnf?millis=1
    public static void requestDataByHttpClientGet(String url, final RequestDataInterface dataInterface) {
        LogUtil.e(TAG, "url==============" + url);
        new AsyncTask<String, Void, Void>() {
            @Override
            protected Void doInBackground(String... params) {
                try {
                    HttpClient client = new DefaultHttpClient();
                    HttpGet get = new HttpGet(params[0]);
                    HttpResponse response = client.execute(get);
                    String value = EntityUtils.toString(response.getEntity());
                    LogUtil.e(TAG, "HttpClientGet==============" + value);
                    dataInterface.getNetData(value);
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(url);
    }


    // 第四中方式是使用HttpClient进行Post方式通信
    // 如url为http://data.tvguo.tv/mobile/commCnf;keyPath为“millis=1”
    public static void requestDataByHttpClientPost(String url, final String keyPath, final RequestDataInterface dataInterface) {
        LogUtil.e(TAG, "url==============" + url);
        LogUtil.e(TAG, "keyPath==============" + keyPath);
        new AsyncTask<String, Void, Void>() {
            @Override
            protected Void doInBackground(String... params) {
                HttpPost post = new HttpPost(params[0]);
                try {
                    HttpClient client = new DefaultHttpClient();
                    StringEntity entity = new StringEntity(keyPath);
                    post.setEntity(entity);
                    HttpResponse response = client.execute(post);
                    String value = EntityUtils.toString(response.getEntity());
                    LogUtil.e(TAG, "HttpClientPost==============" + value);
                    dataInterface.getNetData(value);
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
        }.execute(url);
    }
}
0 0
原创粉丝点击