给你一段代码看看,这是我在一个类里的方法,在异步中调用这个方法,在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;
}