2013-08-30 46 views
0

我试图让我的应用程序与主机PC通信(在真实手机上运行时它仍然是这种情况)。 我的电脑本身与远程服务器通信:我编写了Java类,它们发送HTTP请求并侦听传入的HTTP通知。这些类在我的PC上完美运行。模拟器连接时我的Android应用程序没有Internet访问

我改编了他们一点在我的Android应用程序。但不幸的是,应用程序无法访问互联网:所有请求超时。我仔细检查了授权:在Manifest文件中允许Internet访问,并有效地显示在参数菜单中的应用程序信息中。 我的模拟器具有完整的Internet访问权限:我可以浏览Internet,包括主机PC上的网站。

为了消除个人电脑的问题,我首先尝试获得“http://www.google.fr/index.html”:它完全可以在我的PC上使用相同的类,但不能在模拟器上使用。 然后,我试图按照建议的here模拟器的接口尝试tcpdump流量:我没有任何HTTP请求。

为什么...

我的模拟器运行的是Android 2.3.3(API等级10),我想不通。

这是我的连接类(我试过一个了HTTPClient和有同样的问题): (我知道日志声明“请求发送”是不是在最好的地方,它会被纠正:)

package com.example.myApp; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.SocketTimeoutException; 
import java.net.URL; 
import java.security.InvalidParameterException; 

import android.util.Log; 

public class ServerConnection { 

private String url; 
private String method; 
private String body; 
private String response; 
private int responseCode; 
private int DEFAULT_TIMEOUT = 4 * 1000; 
private int READ_TIMEOUT = 6 * 1000; 
private static String TAG = "ServerConnection"; 

/** 
* Create a new HTTP connection in order to send requests, retrieve responses, delete resources or receive notifications 
* method MUST BE GET, POST or DELETE. 
* @param url 
* @param port 
* @param method 
* @param body 
* @throws InvalidParameterException 
*/ 
public ServerConnection(String url, String method, String body) throws InvalidParameterException{ 

    this.url = url; 
    this.method = method; 
    this.body = body; 

    Log.d(TAG,"ServerConnection instantiated with parameters"); 

    if(this.method == "POST") 
     this.sendPost(); 
    else if (this.method =="GET") 
     this.sendGet(); 
    else if (this.method == "DELETE") 
     this.sendDelete(); 
    else 
     throw new InvalidParameterException("Invalid HTTP method"); 
} 

/** 
* Sends a HTTP request that does not require any body : for example a GET or DELETE 
* @param url 
* @param method 
* @throws InvalidParameterException 
*/ 
public ServerConnection(String url, String method) throws InvalidParameterException{ 

    this.url = url; 
    this.method = method; 

    Log.d(TAG,"HTTP Request instantiated without body"); 

    if (this.method =="GET") 
     this.sendGet(); 
    else if (this.method == "DELETE") 
     this.sendDelete(); 
    else 
     throw new InvalidParameterException("Invalid HTTP method"); 
} 

/** 
* Send a POST request with the specified url, port, body arguments. 
* 
* @return 
*/ 
public void sendPost(){ 
    new Thread(new Runnable() { 
     public void run() { 
      HttpURLConnection connection = null; 
      Log.d(TAG,"Sending HTTP POST Request..."); 

      try{ 
       URL serverUrl = new URL(ServerConnection.this.url); 
       connection = (HttpURLConnection) serverUrl.openConnection(); 
       InputStream is; 

       connection.setDoOutput(true); 
       connection.setDoInput(true); 
       connection.setRequestMethod("POST"); 
       connection.setRequestProperty("Content-Type", "application/xml"); 
       connection.setRequestProperty("Accept", "application/xml"); 
       connection.setRequestProperty("Content-Length","" + Integer.toString(ServerConnection.this.body.getBytes().length)); 
       connection.setUseCaches(false); 
       connection.setInstanceFollowRedirects(false); 
       connection.setConnectTimeout(DEFAULT_TIMEOUT); 
       connection.setReadTimeout(READ_TIMEOUT); 

       connection.connect(); 

       OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream()); 

       writer.write(ServerConnection.this.body); 
       writer.flush(); 
       writer.close(); 

       // Get the response 
       is = connection.getInputStream(); 
       ServerConnection.this.response = getStringFromInputStream(is); 
       ServerConnection.this.responseCode = connection.getResponseCode(); 
      } catch(SocketTimeoutException ex) { 
       ex.printStackTrace(); 
       Log.d(TAG,"SocketTimeoutException"); 
       ServerConnection.this.response = null; 
       return; 
      }catch(MalformedURLException ex) { 
       ex.printStackTrace(); 
       Log.e(TAG,"MalformedURLException"); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
       Log.e(TAG,"IOException"); 
      } finally { 
       connection.disconnect(); 
       Log.d(TAG,"Request sent."); 
      } 
     } 
    }).start(); 

} 



public void sendDelete(){ 

    new Thread(new Runnable() { 
     public void run() { 
      HttpURLConnection connection = null; 
      Log.d(TAG,"Sending HTTP DELETE Request..."); 

      try{ 
       URL serverUrl = new URL(ServerConnection.this.url); 
       connection = (HttpURLConnection) serverUrl.openConnection(); 
       InputStream is; 

       connection.setDoOutput(true); 
       connection.setRequestMethod("DELETE"); 
       connection.setRequestProperty("Accept", "application/xml"); 
       connection.setRequestProperty("Host", ServerConnection.this.url); 
       connection.setConnectTimeout(DEFAULT_TIMEOUT); 
       connection.setReadTimeout(READ_TIMEOUT); 

       connection.connect(); 
       Log.d(TAG,"Connected"); 

       // Get the response 
       is = connection.getInputStream(); 
       ServerConnection.this.response = getStringFromInputStream(is); 
       ServerConnection.this.responseCode = connection.getResponseCode(); 


      } catch(SocketTimeoutException ex) { 
       ex.printStackTrace(); 
       Log.d(TAG,"SocketTimeoutException"); 
       ServerConnection.this.response = null; 
       return; 
      }catch(MalformedURLException ex) { 
       ex.printStackTrace(); 
       Log.d(TAG,"MalformedURLException"); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
       Log.d(TAG,"IOException"); 
      } finally { 
       connection.disconnect(); 
       Log.d(TAG,"Request sent."); 
      } 
     } 
    }); 

} 

public void sendGet(){ 
    new Thread(new Runnable() { 
     public void run() { 
      HttpURLConnection connection = null; 
      Log.d(TAG,"Sending HTTP GET Request..."); 
      try{ 
       InputStream is; 
       URL serverUrl = new URL(ServerConnection.this.url); 
       connection = (HttpURLConnection) serverUrl.openConnection(); 

       connection.setDoOutput(true); 
       connection.setRequestMethod("GET"); 
       connection.setRequestProperty("Accept", "application/xml"); 
       connection.setRequestProperty("Host", ServerConnection.this.url); 
       connection.setConnectTimeout(DEFAULT_TIMEOUT); 
       connection.setInstanceFollowRedirects(false); 

       connection.connect(); 
       Log.d(TAG,"Connecting for GET request"); 

       // Get the response 
       ServerConnection.this.responseCode = connection.getResponseCode(); 
       is = connection.getInputStream(); 
       ServerConnection.this.response = getStringFromInputStream(is); 
       Log.d(TAG,"page received: " + ServerConnection.this.response); 

      } catch(SocketTimeoutException ex) { 
       ex.printStackTrace(); 
       Log.d(TAG,"SocketTimeoutException while sending GET request"); 
       ServerConnection.this.response = null; 
      }catch(MalformedURLException ex) { 
       ex.printStackTrace(); 
       Log.e(TAG,"MalformedURLException"); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
       Log.e(TAG,"IOException"); 
      } finally { 
       connection.disconnect(); 
       Log.d(TAG,"Request sent."); 
      } 
     } 
    }).start(); 
} 

private static String getStringFromInputStream(InputStream is) { 
    Log.d(TAG,"Converting InputStream to response String..."); 
    BufferedReader br = null; 
    StringBuilder sxb = new StringBuilder(); 

    String line; 
    try { 

     br = new BufferedReader(new InputStreamReader(is)); 
     while ((line = br.readLine()) != null) { 
      sxb.append(line); 
     } 

    } catch (IOException e) { 
     Log.e(TAG,"IOException"); 
     e.printStackTrace(); 
    } finally { 
     if (br != null) { 
      try { 
       br.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
       Log.e(TAG,"IOException"); 
      } 
     } 
    } 

    return sxb.toString(); 

} 

public int getResponseCode(){ 
    return this.responseCode; 
} 

    public String getResponse(){ 
     return this.response; 
    } 

    @Override 
     public String toString() { 
     return "CTSConnection [url=" + url + ", method=" + method + ", body=" 
      + body + ", response=" + response + ", responseCode=" 
       + responseCode + "]"; 
     } 

}

当我启动ServerConnection cnx = new ServerConnection("http://www.google.fr/index.html","GET");,我得到以下日志:

08-30 14:42:43.072: D/ServerConnection(581): Sending HTTP GET Request... 
08-30 14:42:43.203: D/ServerConnection(581): Connecting for GET request 
08-30 14:42:43.252: W/System.err(581): java.io.FileNotFoundException: http://www.google.fr/index.html 
08-30 14:42:43.252: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521) 
08-30 14:42:43.252: W/System.err(581): at com.example.myApp.ServerConnection$3.run(ServerConnection.java:205) 
08-30 14:42:43.252: W/System.err(581): at java.lang.Thread.run(Thread.java:1019) 
08-30 14:42:43.252: E/ServerConnection(581): IOException 
08-30 14:42:43.252: D/ServerConnection(581): Request sent. 

对于POST请求(凭有效的身体),用于考试PLE ServerConnection cnx = new SertverConnection(10.0.2.2:65005,"POST","xml_document_to_string")

08-30 14:42:43.299: D/ServerConnection(581): Sending HTTP POST Request... 
08-30 14:42:47.348: W/System.err(581): java.net.SocketTimeoutException: Connection timed out 
08-30 14:42:47.365: W/System.err(581): java.net.SocketTimeoutException: Connection timed out 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method) 
08-30 14:42:47.365: W/System.err(581): at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437) 
08-30 14:42:47.365: W/System.err(581): at java.net.Socket.connect(Socket.java:983) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:75) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
08-30 14:42:47.365: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 
08-30 14:42:47.372: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 
08-30 14:42:47.372: W/System.err(581): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 
08-30 14:42:47.372: W/System.err(581): at com.example.myApp.ServerConnection$1.run(ServerConnection.java:102) 
08-30 14:42:47.382: W/System.err(581): at java.lang.Thread.run(Thread.java:1019) 
08-30 14:42:47.382: D/ServerConnection(581): SocketTimeoutException 
08-30 14:42:47.382: D/ServerConnection(581): Request sent. 

嗯,我真的无法揣摩出这个问题来自......任何提示?

在此先感谢!

+0

你有清单许可吗? – Selvin

+0

是的,我有权限,它出现在仿真器上的应用程序信息中。 – er11

回答

0

需要编辑默认的APN详细信息才能让互联网在应用程序中工作。 您可以将设置这样做>无线&网络>移动网络>默认APN

在我而言,我不得不提供代理端口。我还必须清除用户名 & 密码字段作为我的代理没有。

这使我的模拟器上的应用程序访问互联网。

相关问题