android客户端通过get,post进行数据交互
来源:互联网 发布:数据分类汇总步骤 编辑:程序博客网 时间:2024/04/30 13:09
【get方式】
protected String getRequest(String url, DefaultHttpClient client) throws Exception {
DefaultHttpClient client = new DefaultHttpClient(new BasicHttpParams());
//超时请求
client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
//读取超时
client.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
String result = null;
int statusCode = 0;
HttpGet getMethod = new HttpGet(url);
try {
HttpResponse httpResponse = client.execute(getMethod);
//statusCode == 200 正常
statusCode = httpResponse.getStatusLine().getStatusCode();
//处理返回的httpResponse信息
result = retrieveInputStream(httpResponse.getEntity());
} catch (Exception e) {
} finally {
getMethod.abort();
}
return result;
}
/**
* 处理httpResponse信息,返回String
*
* @param httpEntity
* @return String
*/
protected String retrieveInputStream(HttpEntity httpEntity) {
Long l = httpEntity.getContentLength();
int length = (int) httpEntity.getContentLength();
//the number of bytes of the content, or a negative number if unknown. If the content length is known but exceeds Long.MAX_VALUE, a
negative number is returned.
//length==-1,下面这句报错,println needs a message
//System.out.println("length = "+length);
if (length < 0) length = 10000;
StringBuffer stringBuffer = new StringBuffer(length);
try {
InputStreamReader inputStreamReader = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8);
char buffer[] = new char[length];
int count;
while ((count = inputStreamReader.read(buffer, 0, length - 1)) > 0) {
stringBuffer.append(buffer, 0, count);
}
}catch(IOException e){
e.printStackTrace();
}
return stringBuffer.toString();
}
【post方式】
//////
List<NameValuePair> nameValuePairs = new List<NameValuePair>();//构建post给php的参数
nameValuePairs.add(new BasicNameValuePair("key","value"));
//通过post获得数据
public static String postHttpData(String url,List<NameValuePair> nameValuePairs)
{
String resultStr=null;
HttpClient httpclient = new DefaultHttpClient();
//超时请求
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
//读取超时
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 5000);
HttpPost httppost = new HttpPost(url);
try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response;
response=httpclient.execute(httppost);
resultStr=retrieveInputStream(httpResponse.getEntity());
} catch (UnsupportedEncodingException e) {
Log.d(url, "UnsupportedEncodingException");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d(url, "ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.d(url, "IOException");
e.printStackTrace();
}
return resultStr;
}
get的参数设置是有长度限制的,在用json传递大数据量时,get方式很可能超过长度。
post的参数没有长度限制,可以放心使用。
注意超时的部分,网络不稳定或是其他情况,可是非常有必要的,
不然android客户端卡住就影响用户体验了。
- android客户端通过get,post进行数据交互
- Android客户端通过GET和POST向服务器发送数据
- Android通过get,post方式客户端与服务器端交互实例(慕课网笔记)
- Unity3D 通过Get与Post方式与服务器端进行交互
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互
- Android通过Post与Get提交数据
- Android通过Post与Get提交数据
- Android开发:客户端与服务器通过传递和接收json数据进行交互
- 程序使用 HTTP 协议和服务器交互主要是进行数据的提交,通常数据的提交是通过 GET 和 POST 两种方式来完成
- Android客户端与PC服务器通过socket进行交互实例
- Android客户端与PC服务器通过socket进行交互实例
- Android客户端与PC服务器通过socket进行交互实例
- Android客户端与PC服务器通过socket进行交互实例
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- 通过url,post get数据
- android 通过get,post方式提交数据给服务器
- Java学习笔记之FreeTTS(语音)
- 培训之路四——项目管理
- win7 vs2008 release mt.exe停止工作
- zergRush源码
- ALSA之PCM分析
- android客户端通过get,post进行数据交互
- HashMap的两种排序方式
- ubuntu11.10下nfs(Network File System)配置
- wsgen与wsimport命令说明
- Webservice:客户端用https
- Oracle数据库中大数据量查询优化问题 -- 分区表的操作方法
- 英制和公制
- 基于数论变换的大整数乘法Java代码实现
- Repeater绑定ul每五个li换一次行