0

I came across from ConnectivityManager and wifi . But it doesn't solve my problem. We are from lower internet bandwidth countries. Although data service is activated, there is no internet connection in some(every)time .

So , data service connected and wifi connected conditions can not able to decide whether our devices having an active internet connection .

.. So, I tried http post with AsyncTask . But it can't catch no active connection. But works well while in active connection.

here is my code -

  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);
        }
       }
    }
}

All Catch process not work when no internet connection. Please kindly help me. I want to toast "No Internet connection" if http post can't find active connection .

Htet Honey
  • 11
  • 2
  • possible duplicate of [How to handle slow network connection in Java/Android](http://stackoverflow.com/questions/11942643/how-to-handle-slow-network-connection-in-java-android) – David Passmore Jul 03 '15 at 20:54
  • it is not duplicated. I would like to toast "No Internet connection" if http post can't find active connection . trying to work catch{ } . – Htet Honey Jul 03 '15 at 21:06

1 Answers1

0

Please correct me if I am wrong, but your questions boil down to:

  • is there a valid network connection

    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
    ...
    
  • and if the connection is not wifi but some other type - fast / not fast

    // 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
    
Dimitar Genov
  • 2,056
  • 13
  • 11
  • They are not very specific to ConnectivityManager and also github speed test. I want to test my connection like pinging or httposting to return something. your answer is perfect enough but we are from very slower internet bandwidth countries. – Htet Honey Jul 03 '15 at 21:24