Android 如果我使用移动数据而不是 wifi,则无法使用

Android doesn't work if I use mobile data instead of wifi

当我启动 android 应用程序并尝试使用 WiFi 数据进行登录过程时,一切正常。 但是,如果我再次启动该应用程序并尝试使用移动数据执行相同的进程,该进程仍在等待,直到出现错误。

密码是:

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

首先,我认为这是由于权限问题,但我补充说:

<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"/>

然后,Logcat returns 我:[更新]

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.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at android.os.AsyncTask$SerialExecutor.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)

我认为,权限没有问题。

问题是我不知道是因为流程多长还是其他方面的问题[超时错误]我在网上找但是我没有不知道。

谢谢!

192.168.* 地址属于专用网络,因此无法从 Internet 访问。请参阅有关 Wikipedia 的文章,所谓的 16 位块。

它在您使用 WiFi 时工作,因为您在网络上并通过输入它,它会将它转发到路由器,路由器将提供相应的页面(请注意请求从未真正到达互联网).然而,当使用移动数据时,没有路由器,只有不实现此接口的手机信号塔,您最终将尝试查找 Internet 上不存在的页面。加载很可能会挂起(某些东西可以识别地址,但它无法提供请求的页面),直到连接超时,导致给定的异常。

没有进一步的上下文,我无法提供任何解决方案。