2015-07-03 66 views
0

我碰到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連接」。

+0

[如何處理Java/Android中的慢速網絡連接]可能的重複(http://stackoverflow.com/questions/11942643/how-to-handle-slow-network-connection-in-java-android) –

+0

它是不重複。如果http post找不到活動連接,我想烤麪包「沒有Internet連接」。嘗試工作catch {}。 –

回答

0

請糾正我,如果我錯了,但你的問題可以歸結爲:

  • 是有一個有效的網絡連接

    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 
    
+0

它們不是特定於ConnectivityManager和github速度測試。我想測試我的連接,如ping或httposting返回的東西。你的答案非常完美,但我們來自非常慢的互聯網帶寬國家。 –