Android doesn't work if I use mobile data inst

2019-06-13 03:53发布

问题:

When I launch my android app and I try to do a login process using WiFi data, it's all right. But if I launch the app again and I try to to the same process using mobile data, the process still waiting until I get an error.

The code is:

class AsyncLogin extends AsyncTask< String, String, String > {
    String username, password;

    protected void onPreExecute() {
        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    protected String doInBackground(String... params) {
        username = params[0];
        password = params[1];

        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost post = new HttpPost(URI_login);
        post.setHeader("content-type", "application/json");

        try {
            JSONObject jpost = new JSONObject();
            jpost.put("password", password);
            jpost.put("username", username);

            StringEntity entity = new StringEntity(jpost.toString(),"UTF-8"); 
            entity.setContentEncoding("UTF-8");
            entity.setContentType("application/json");

            post.setEntity(entity);
            HttpResponse resp = httpClient.execute(post);
            resultado = EntityUtils.toString(resp.getEntity());

            System.out.println("Nos llega: "+ resultado.toString() +"");

          **//UPDATE, I was returning null**
        } catch (JSONException e) {
            e.printStackTrace();
            return "e-json"; //
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "e-encoding";
        } catch (ClientProtocolException e) {
            e.printStackTrace();
            return "e-client";
        } **catch (IOException e) {   ***//Now, this is the error***
            e.printStackTrace();
            return "e-io";**
        }

        return resultado.toString();

    }
    protected void onPostExecute(String result) {
       pDialog.dismiss();
       Log.e("onPostExecute=",""+result);

       if (result.equals("ok")){
           Intent i = new Intent(MainActivity.this, HiScreen.class);
           i.putExtra("user_hi", username);
           Log.e("onPostExecute= ", "Login is correct");
           login_ok();
           startActivity(i);
       }else{
            Log.e("onPostExecute= ", result);
       }
    }

Firstly, I thought that it would be due to permissions but I added:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>

and then, the Logcat returns me: [UPDATE]

org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.1.104:8080 refused at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183) 
        at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:374)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:575)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:498)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:476)
    at com.josvalbae.androidrest_1.MainActivity$AsyncLogin.doInBackground(MainActivity.java:196)
    at com.josvalbae.androidrest_1.MainActivity$AsyncLogin.doInBackground(MainActivity.java:1)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)
    Caused by: java.net.ConnectException: failed to connect to /192.168.1.104 (port 8080): connect failed: ETIMEDOUT (Connection timed out)
    Caused by: android.system.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)

I think, there is no problem with permissions.

The problem is that I don't know if is due to how long the process or other aspect.[Timeout error] I was looking for on the Internet but I don't have any idea.

Thanks!

回答1:

192.168.* addresses belong to private networks and therefore are not accessible from the Internet. See the article on Wikipedia, so called 16-bit block.

It works when you're on WiFi, because you're on the network and by typing it in, it will forward it to the router, which will serve the appropriate page (notice how request never really reaches the Internet). When on mobile data, however, there are no routers, just cell towers which don't implement this interface and you'll end up trying to look up page which doesn't exist on the Internet. The loading most probably hangs (something recognizes the address, but it can't provide the requested page) until connection times out, causing the given Exception.

Without further context, I cannot provide any solution.