android Http 网络超时,怎么设置
2014-10-19 16:04
337 查看
给你一段代码看看,这是我在一个类里的方法,在异步中调用这个方法,在handler里取得返回值,进行处理。
public static String addCustDetails(TCUSTINFODETAILS1 custdetail){
HttpPost httpPost=new HttpPost(URLD);//webservice地址
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("MGUID",custdetail.getMGUID()));
params.add(new BasicNameValuePair("FollowDate",custdetail.getFollowDate()));
params.add(new BasicNameValuePair("FollowTitle",custdetail.getFollowTitle()));
params.add(new BasicNameValuePair("Contacter",custdetail.getContacter()));
params.add(new BasicNameValuePair("Follower",custdetail.getFollower()));
params.add(new BasicNameValuePair("QiatanType",custdetail.getQiatanType()));
params.add(new BasicNameValuePair("Qingkuang",custdetail.getQingkuang()));
params.add(new BasicNameValuePair("biztype",custdetail.getBiztype()));
params.add(new BasicNameValuePair("itemclass",custdetail.getItemclass()));
params.add(new BasicNameValuePair("ItemBarnd",custdetail.getItembrand()));
params.add(new BasicNameValuePair("Mobile",custdetail.getMobile()));
params.add(new BasicNameValuePair("Mail",custdetail.getMail()));
params.add(new BasicNameValuePair("SIMID",custdetail.getSIMID()));
HttpResponse httpResponse;
String code = "";
String code2="";
int code1=0;
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpClient client=new DefaultHttpClient();
HttpParams parms=client.getParams();
HttpConnectionParams.setConnectionTimeout(parms, 6000);//设置网络超时
HttpConnectionParams.setSoTimeout(parms, 45000);//设置网络超时
httpResponse=client.execute(httpPost);
code1=httpResponse.getStatusLine().getStatusCode();
String strResult=EntityUtils.toString(httpResponse.getEntity());
String JsonStr="{CUSTINFOD:["+strResult+"]}";
JSONObject jsonobject=new JSONObject(JsonStr.toString());
JSONArray arrayJson=jsonobject.getJSONArray("CUSTINFOD");
if(code1==201){
code2="";//返回成功
}else{
for(int i=0;i<arrayJson.length();i++){
JSONObject Json=arrayJson.optJSONObject(i);
code2=Json.getString("Message");//返回错误信息
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
code1=200000;
code2=e.getMessage();//返回其他错误信息
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
code1=100000;
code2="网络连接有问题,请检查";//这个也是捕获超时的,但最终返回错误的是下面一句
} catch (IOException e) {
// TODO Auto-generated catch block
code1=100000;
code2="网络连接有问题,请检查";//这里捕获超时
} catch (JSONException e) {
// TODO Auto-generated catch block
code1=200001;
code2=e.getMessage();//返回JSON错误
}
code=code1+":"+code2;
return code;
}
public static String addCustDetails(TCUSTINFODETAILS1 custdetail){
HttpPost httpPost=new HttpPost(URLD);//webservice地址
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("MGUID",custdetail.getMGUID()));
params.add(new BasicNameValuePair("FollowDate",custdetail.getFollowDate()));
params.add(new BasicNameValuePair("FollowTitle",custdetail.getFollowTitle()));
params.add(new BasicNameValuePair("Contacter",custdetail.getContacter()));
params.add(new BasicNameValuePair("Follower",custdetail.getFollower()));
params.add(new BasicNameValuePair("QiatanType",custdetail.getQiatanType()));
params.add(new BasicNameValuePair("Qingkuang",custdetail.getQingkuang()));
params.add(new BasicNameValuePair("biztype",custdetail.getBiztype()));
params.add(new BasicNameValuePair("itemclass",custdetail.getItemclass()));
params.add(new BasicNameValuePair("ItemBarnd",custdetail.getItembrand()));
params.add(new BasicNameValuePair("Mobile",custdetail.getMobile()));
params.add(new BasicNameValuePair("Mail",custdetail.getMail()));
params.add(new BasicNameValuePair("SIMID",custdetail.getSIMID()));
HttpResponse httpResponse;
String code = "";
String code2="";
int code1=0;
try {
httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpClient client=new DefaultHttpClient();
HttpParams parms=client.getParams();
HttpConnectionParams.setConnectionTimeout(parms, 6000);//设置网络超时
HttpConnectionParams.setSoTimeout(parms, 45000);//设置网络超时
httpResponse=client.execute(httpPost);
code1=httpResponse.getStatusLine().getStatusCode();
String strResult=EntityUtils.toString(httpResponse.getEntity());
String JsonStr="{CUSTINFOD:["+strResult+"]}";
JSONObject jsonobject=new JSONObject(JsonStr.toString());
JSONArray arrayJson=jsonobject.getJSONArray("CUSTINFOD");
if(code1==201){
code2="";//返回成功
}else{
for(int i=0;i<arrayJson.length();i++){
JSONObject Json=arrayJson.optJSONObject(i);
code2=Json.getString("Message");//返回错误信息
}
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
code1=200000;
code2=e.getMessage();//返回其他错误信息
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
code1=100000;
code2="网络连接有问题,请检查";//这个也是捕获超时的,但最终返回错误的是下面一句
} catch (IOException e) {
// TODO Auto-generated catch block
code1=100000;
code2="网络连接有问题,请检查";//这里捕获超时
} catch (JSONException e) {
// TODO Auto-generated catch block
code1=200001;
code2=e.getMessage();//返回JSON错误
}
code=code1+":"+code2;
return code;
}
相关文章推荐
- HttpURLConnection中如何设置网络超时
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- 如何设置android HttpPost 连接服务器超时
- HttpURLConnection中如何设置网络超时
- android httpurlconnection或者httpclient总是超时,服务器正常,但网络正常,浏览器可以上网
- Android http请求数据 设置超时
- android httpurlconnection或者httpclient总是超时,服务器正常,但网络正常,浏览器可以上网
- Android在Http连接中设置超时和代理?
- android 中设置HttpURLConnection 超时并判断是否超时
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- Android HttpGet和HttpPost设置超时
- Android HttpGet和HttpPost设置超时
- Android HttpGet,HttpPost,HttpPut设置超时
- HttpURLConnection中如何设置网络超时
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- android httpclient 设置超时
- Android Http请求网络模拟超时
- android - DefaultHttpClient设置超时.
- Android在Http连接中设置超时和代理
- Android HttpGet和HttpPost设置超时