当我启动我的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返回我: [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)
我认为,权限没有问题。
问题在于,我不知道是否由于过程或其他方面有多长。 [超时错误] 我在互联网上寻找但是我没有&#39;有任何想法。
谢谢!
答案 0 :(得分:2)
192.168.*
个地址属于专用网络,因此无法从Internet访问。请参阅Wikipedia上的文章,即所谓的16位块。
当你使用WiFi时,它会起作用,因为你在网络上并输入它,它会将它转发到路由器,路由器将提供适当的页面(注意请求永远不会真正到达互联网)。但是,在移动数据上,没有路由器,只有单元塔没有实现此接口,您最终将尝试查找Internet上不存在的页面。加载最有可能挂起(某些东西识别地址,但它无法提供请求的页面),直到连接超时,导致给定的异常。
如果没有进一步的背景,我无法提供任何解决方案。