我是从ConnectivityManager和wifi过来的。但这并不能解决我的问题。我们来自互联网带宽较低的国家。虽然数据服务已开通,但在某些(每次)时间内没有互联网连接。
因此,数据服务连接和wifi连接的条件不能决定我们的设备是否具有活动的互联网连接。
。。因此,我尝试了使用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找不到活动的连接,我想干杯“没有互联网连接”。
发布于 2015-07-04 05:16:05
如果我错了,请纠正我,但你的问题可以归结为:
context Context = getContext();上下文管理器= ( ConnectivityManager ) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo networkInfo = manager.getActiveNetworkInfo();networkInfo = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()?info : null;//如果networkInfo不是null,则您有网络,否则将引发错误...
//从上面继续,int type = networkInfo.getType();int subtype = networkInfo.getSubtype();boolean isFastConnection = false;if (类型== ConnectivityManager.TYPE_WIFI ||类型== ConnectivityManager.TYPE_ETHERNET) { isFastConnection = true;} else if (类型为== ConnectivityManager.TYPE_MOBILE) { switch (子类型){ case TelephonyManager.NETWORK_TYPE_1xRTT: case TelephonyManager.NETWORK_TYPE_CDMA: case TelephonyManager.NETWORK_TYPE_EDGE: case TelephonyManager.NETWORK_TYPE_GPRS: case TelephonyManager.NETWORK_TYPE_IDEN: isFastConnection = false;break;case TelephonyManager.NETWORK_TYPE_EVDO_0: case TelephonyManager.NETWORK_TYPE_EVDO_A: case TelephonyManager.NETWORK_TYPE_HSDPA: case TelephonyManager.NETWORK_TYPE_HSPA: case TelephonyManager.NETWORK_TYPE_HSUPA: case TelephonyManager.NETWORK_TYPE_UMTS: case TelephonyManager.NETWORK_TYPE_EVDO_B: // API 11 case TelephonyManager.NETWORK_TYPE_EHRPD:案例TelephonyManager.NETWORK_TYPE_LTE: // API 13案例TelephonyManager.NETWORK_TYPE_HSPAP: isFastConnection = true;break;default: isFastConnection = false;break;}} //为您的案例处理isFastConnection布尔值
https://stackoverflow.com/questions/31214067
复制相似问题