Android连接超时异常
下面是代码,我得到例外(超时),请提供任何解决方案或教程相同。在4.0.4 api级设备上使用Android连接超时异常,android,web-services,http,timeout,Android,Web Services,Http,Timeout,下面是代码,我得到例外(超时),请提供任何解决方案或教程相同。在4.0.4 api级设备上使用 HttpClient client = new DefaultHttpClient(); HttpPost request = new HttpPost(url); List<NameValuePair> params = new LinkedList<NameValuePair>(); > param has some values and a string
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> params = new LinkedList<NameValuePair>();
> param has some values and a string of bitmap.
HttpResponse response = client.execute(request);
StringBuilder receivedData = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null)
{
receivedData.append(line);
}
HttpClient=newdefaulthttpclient();
HttpPost请求=新的HttpPost(url);
List params=new LinkedList();
>param有一些值和一个位图字符串。
HttpResponse response=client.execute(请求);
StringBuilder receivedData=新建StringBuilder();
BufferedReader=new BufferedReader(new InputStreamReader(response.getEntity().getContent());
字符串行=null;
而((line=reader.readLine())!=null)
{
receivedData.append(行);
}
试试这个:
HttpConnectionParams.setConnectionTimeout(httpParams,
TIMEOUT_MILLISEC);
HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
首先,您需要了解什么是connectiontimeoutexception: 连接到HTTP服务器或等待来自HttpConnectionManager的可用连接时超时 因此,这意味着您的设备的internet无法与HTTP服务器建立连接,并且请求超时。您可以做以下两件事来避免这种情况:
public static Boolean checkActiveInternet(Context activity) {
ConnectivityManager cm = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else if (netInfo != null&& (netInfo.getState() == NetworkInfo.State.DISCONNECTED|| netInfo.getState() == NetworkInfo.State.DISCONNECTING|| netInfo.getState() == NetworkInfo.State.SUSPENDED || netInfo.getState() == NetworkInfo.State.UNKNOWN)) {
return false;
} else {
return false;
}
}HttpGet httpget = new HttpGet(url);
HttpResponse response;
HttpParams httpParameters = new BasicHttpParams();
int timeoutConnection = 60 * 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
int timeoutSocket = 60 * 1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
try {
response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
result = convertStreamToString(instream);
// instream.close();
}
} catch (ClientProtocolException e) {
Utilities.showDLog("connect", "ClientProtocolException:-" + e);
} catch (IOException e) {
Utilities.showDLog("connect", "IOException:-" + e);
}
return result;
}使用AsyncTask并在中执行所有流程background@zalajanaksinh:这对我不起作用,先生……开始用谷歌搜索bgprocee的例子吧,它用的也是wotk亲爱的。