四种网络数据请求汇总
来源:互联网 发布: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);
}
}
//网络请求数据有多种方式,以下汇总了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
- 四种网络数据请求汇总
- Android 网络请求汇总
- 四种网络请求方式详解
- android网络请求框架汇总
- Android 网络请求框架汇总
- 两种方式综合请求网络数据
- 网络数据请求
- qml 请求网络数据
- 网络请求数据解析
- iOS网络数据请求
- 请求网络数据JSON
- 网络请求数据
- 网络请求数据
- 网络数据请求
- 网络异步请求数据
- IOS_UI_网络数据请求
- 从网络请求数据
- 网络数据的请求
- goLang gob 编码
- job_queue_processes 文档 ID 1491941.1
- swiper轮播图代码实例
- javaweb数据库批处理问题
- LTE RF PICS
- 四种网络数据请求汇总
- 第七章上机练习3
- shell历史
- Celery:分布式任务队列 简单上手
- php 排序源码
- 动态代理(设计模式)
- drbd+keepalived nfs高可用方案实践
- zlib://封装协议
- Android Studio你不知道的调试技巧