Android httppost与AsyncTask crush应用程序在较低的互联网带宽
我是从ConnectivityManager和wifi公司来的。但这并不能解决我的问题。我们来自互联网带宽较低的国家。虽然数据服务已激活,但在某些(每次)时间内没有internet连接 因此,数据服务连接和wifi连接条件无法决定我们的设备是否具有活动的互联网连接 。。 所以,我尝试了http post和AsyncTask。但它无法捕获任何活动连接。但在主动连接中工作良好 这是我的密码-Android httppost与AsyncTask crush应用程序在较低的互联网带宽,android,http-post,Android,Http Post,我是从ConnectivityManager和wifi公司来的。但这并不能解决我的问题。我们来自互联网带宽较低的国家。虽然数据服务已激活,但在某些(每次)时间内没有internet连接 因此,数据服务连接和wifi连接条件无法决定我们的设备是否具有活动的互联网连接 。。 所以,我尝试了http post和AsyncTask。但它无法捕获任何活动连接。但在主动连接中工作良好 这是我的密码- class RequestTask extends AsyncTask<String, Strin
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);
}
}
}
}
class RequestTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…uri){
字符串responseString=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse响应;
response=httpclient.execute(新的HttpGet(uri[0]);
StatusLine StatusLine=response.getStatusLine();
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
responseString=out.toString();
out.close();
}否则{
//关闭连接。
//response.getEntity().getContent().close();
Toast.makeText(getApplicationContext(),“无Internet连接:”,Toast.LENGTH\u SHORT.show();
}
}捕获(客户端协议例外e){
//处理问题。。
Toast.makeText(getApplicationContext(),“无Internet连接:”,Toast.LENGTH\u SHORT.show();
}捕获(IOE异常){
//处理问题。。
Toast.makeText(getApplicationContext(),“无Internet连接:”,Toast.LENGTH\u SHORT.show();
}
捕获(例外e){
Toast.makeText(getApplicationContext(),“无Internet连接:”,Toast.LENGTH\u SHORT.show();
}
回报率;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
if(result.equals(“null”)| | result.equals(“”){
Toast.makeText(getApplicationContext(),“找不到帐户:”,Toast.LENGTH\u SHORT.show();
}
否则{
getpass=结果;
Toast.makeText(getApplicationContext(),“连接到服务器:”,Toast.LENGTH\u SHORT.show();
if(getpass.equals(edtpass.getText().toString())){
新建RequestTaskname()。执行(“http://www.yangoninnovation.com/*****?***=“+电子邮件);
}
}
}
}
没有internet连接时,所有捕获进程都不工作。请帮助我。如果http post找不到活动连接,我想敬酒“没有Internet连接” 如果我错了,请纠正我,但你的问题可以归结为:
- 是否存在有效的网络连接
Context context = getContext(); ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = manager.getActiveNetworkInfo(); networkInfo = networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected() ? info : null; // if networkInfo is not null, you have network, otherwise raise your error ...
- 如果连接不是wifi而是其他类型-快速/不快速
// continue from above int type = networkInfo.getType(); int subtype = networkInfo.getSubtype(); boolean isFastConnection = false; if (type == ConnectivityManager.TYPE_WIFI || type == ConnectivityManager.TYPE_ETHERNET) { isFastConnection = true; } else if (type == ConnectivityManager.TYPE_MOBILE) { switch (subtype) { 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: case TelephonyManager.NETWORK_TYPE_LTE: // API 13 case TelephonyManager.NETWORK_TYPE_HSPAP: isFastConnection = true; break; default: isFastConnection = false; break; } } // deal with isFastConnection boolean for your cases