则Android无法正常工作

则Android无法正常工作

本文介绍了如果我使用移动数据代替wifi,则Android无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我启动我的android应用程序并尝试使用WiFi数据进行登录过程时,就可以了.但是,如果我再次启动该应用程序,并尝试使用移动数据执行相同的过程,则该过程仍将等待直到出现错误.

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.

代码是:

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

然后Logcat返回我: [UPDATE]

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)

我认为权限没有问题.

问题是我不知道是否是由于该过程或其他方面而导致的. [超时错误] ,我一直在互联网上寻找,但不知道

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.

谢谢!

推荐答案

192.168.*地址属于专用网络,因此无法从Internet访问.请参阅维基百科上的文章,即所谓的16位块.

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.

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

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.

这篇关于如果我使用移动数据代替wifi,则Android无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 16:54