我有以下用于执行HTTP Post的代码:
InputStream ins = null;
String result = null;
public String postforBakup(String url){
java.util.Date date= new java.util.Date();
Timestamp timestamp = (new Timestamp(date.getTime()));
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 5000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
DefaultHttpClient http = new DefaultHttpClient(httpParameters);
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
try{
JSONArray json = (makeJSON());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(5);
nameValuePairs.add(new BasicNameValuePair("key", processKey()));
nameValuePairs.add(new BasicNameValuePair("type", "Android"));
nameValuePairs.add(new BasicNameValuePair("timestamp", timestamp.toString()));
nameValuePairs.add(new BasicNameValuePair("mail", mail));
nameValuePairs.add(new BasicNameValuePair("jsonArray", Arrays.asList(json).toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse resp = http.execute(httppost);
HttpEntity entity = resp.getEntity();
ins = entity.getContent();
BufferedReader bufread = new BufferedReader(new InputStreamReader(ins, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = bufread.readLine()) != null){
sb.append(line +"\n");
}
result = sb.toString().trim();
System.out.println("Response: "+result);
}catch (Exception e){
System.out.println("Squish: "+e);
}finally{
try{
if(ins != null){
ins.close();
}
}catch(Exception smash){
System.out.println("Squish: "+smash);
}
}
if(result.equalsIgnoreCase("Authentication failed")){
System.out.println("Death");
return "Backup failed";
}else if (result.equalsIgnoreCase("update successfull")){
System.out.println("Life");
return "Success";
}else{
return "failed";
}
}
虽然如果连接到WIFI时该功能正常工作,但当我切换到2g时,我得到加载器几个小时,没有任何异常或超时消息。这可能有什么问题?