我碰到ConnectivityManager和wifi。但它不能解決我的問題。我們來自較低的互聯網帶寬國家。儘管數據服務已激活,但某些(每個)時間都沒有互聯網連接。與較低的互聯網帶寬的AsyncTask粉碎應用httppost
因此,數據服務連接和WiFi連接條件無法確定我們的設備是否具有活動的Internet連接。
.. 所以,我嘗試了與AsyncTask的http post。但它不能捕捉到任何活動的連接。但在主動連接時運行良好。
這裏是我的代碼 -
class RequestTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
String responseString = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString = out.toString();
out.close();
} else{
//Closes the connection.
//response.getEntity().getContent().close();
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
//TODO Handle problems..
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(getApplicationContext(),"No Internet Connection :", Toast.LENGTH_SHORT).show();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result.equals("null") || result.equals("")){
Toast.makeText(getApplicationContext(),"Account Not Found : ", Toast.LENGTH_SHORT).show();
}
else{
getpass=result;
Toast.makeText(getApplicationContext(),"Connecting to Server :", Toast.LENGTH_SHORT).show();
if(getpass.equals(edtpass.getText().toString())){
new RequestTaskname().execute("http://www.yangoninnovation.com/*****?****="+email);
}
}
}
}
所有捕撈過程中不工作的時候沒有互聯網連接。請請幫助我。如果http post找不到活動連接,我想烤麪包「沒有Internet連接」。
[如何處理Java/Android中的慢速網絡連接]可能的重複(http://stackoverflow.com/questions/11942643/how-to-handle-slow-network-connection-in-java-android) –
它是不重複。如果http post找不到活動連接,我想烤麪包「沒有Internet連接」。嘗試工作catch {}。 –