2013-02-21 144 views
1

我想使用本地代理从我的Android设备发送http请求。 在详细描述我的问题之前,我希望您知道在我尝试过的每个设备上运行良好,除了在Android 4.0.4下运行的Xperia arc S和Xperia T之外。我在两台设备上都有数据连接,两者都可以使用!无法解析主机Android

我已经使用了DefaultHttpClient实例来送我的请求,但是当代码到达该部分:

client.execute(request.target, request.post); 

坠毁说以下错误:

02-21 15:37:25.677: W/System.err(1926): java.net.UnknownHostException: Unable to resolve host "192.168.010.200": No address associated with hostname 
02-21 15:37:25.681: W/System.err(1926):  at java.net.InetAddress.lookupHostByName(InetAddress.java:426) 
02-21 15:37:25.681: W/System.err(1926):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242) 
02-21 15:37:25.681: W/System.err(1926):  at java.net.InetAddress.getAllByName(InetAddress.java:220) 
02-21 15:37:25.681: W/System.err(1926):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143) 
02-21 15:37:25.681: W/System.err(1926):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 
02-21 15:37:25.681: W/System.err(1926):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) 
02-21 15:37:25.681: W/System.err(1926):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 
02-21 15:37:25.681: W/System.err(1926):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:580) 
02-21 15:37:25.681: W/System.err(1926):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:534) 
02-21 15:37:25.681: W/System.err(1926):  at com.myapp.mms.sender.WapClient.execute(WapClient.java:59) 

在上面的错误日志,本地IP是我的本地代理主机。

这里是我的全部代码:

public Response execute(PostRequest request) throws Exception 
{ 
    // Set proxy and route 
    this.params = client.getParams(); 
    if (isProxySet) ConnRouteParams.setDefaultProxy(params, new HttpHost(this.host, this.port));   
    request.post.setParams(params); 

    // Set header 
    request.post.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic"); 
    request.post.addHeader("Accept-Language", getCurrentAcceptLanguage(Locale.getDefault())); 

    // Execute the request 
    HttpResponse httpResponse = client.execute(request.target, request.post); 

    // Create the object that will receive the response 
    Response response = new Response(); 

    // Get the response code 
    StatusLine status = httpResponse.getStatusLine(); 
    response.code = status.getStatusCode(); 

    // Get the response body 
    byte[] responseBody = null; 
    HttpEntity entity = httpResponse.getEntity(); 
    if (entity != null) 
    { 
     if (entity.getContentLength() > 0) 
     { 
      responseBody = new byte[(int)entity.getContentLength()]; 
      DataInputStream dis = new DataInputStream(entity.getContent()); 
      dis.readFully(responseBody); 
      dis.close(); 
     } 
     entity.consumeContent(); 
    } 
    response.body = responseBody; 

    return response; 
} 

有没有办法太旁路这个错误只出现在的Android 4.0.4还是我做错了?

或者有没有办法做同样的事情没有调用InetAddress.getByName?

+0

你已经知道地址了吗?看起来有点像你传递它作为一个字符串?这没有必要。另外,你有没有试图摆脱10的领先0? – 2013-02-21 14:59:26

+1

你提到你有一个数据连接是这个WiFi连接或其他什么?我相信192.168。*。*子网仅用于内部路由IP。所以3g/4g/etc连接不可能工作,但无线网络连接将会是 – 2013-02-21 15:07:49

+0

是的,我把这个地址作为一个字符串传递。但'setDefaultProxy'接受'HttpHost'实例。我该怎么办呢?我不明白你的第二个问题... – Manitoba 2013-02-21 15:08:34

回答

4

我发现,似乎每个设备我有工作的方式:我希望这将帮助一些人用同样的埃罗

URL url = new URL(apn); 

    // Set-up proxy 
    Log.d(TAG, "Setting up proxy (" + host + ":" + port + ")"); 
    Properties systemProperties = System.getProperties(); 
    systemProperties.setProperty("http.proxyHost", host); 
    systemProperties.setProperty("http.proxyPort", String.valueOf(port)); 
    systemProperties.setProperty("http.keepAlive", "false"); 

    // Open connection 
    HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 

    // Use the URL connection for output 
    connection.setUseCaches(false); 
    connection.setDoOutput(true); 

    // Set the timeouts 
    connection.setConnectTimeout(TIMEOUT); 
    connection.setReadTimeout(TIMEOUT); 

    // Sends the request 
    connection.setRequestProperty("Content-Type", "application/vnd.wap.mms-message"); 
    connection.setRequestProperty("Content-Length", Integer.toString(mms.length)); 

    // Connect to the MMSC 
    Log.d(TAG, "Connecting to APN " + apn); 
    connection.connect(); 

    try 
    { 
     // Upload mms as bytes array 
     Log.d(TAG, "Uploading data (Size: " + mms.length); 
     OutputStream out = connection.getOutputStream(); 
     out.write(mms); 
     out.flush(); 
     out.close(); 

     // Wait for the response 
     Log.d(TAG, "Response code is " + connection.getResponseCode()); 

     if (connection.getContentLength() >= 0) 
     { 
      Log.d(TAG, "Reading response ..."); 
      byte[] responseArray = new byte[connection.getContentLength()]; 
      DataInputStream i = new DataInputStream(connection.getInputStream()); 
      int b = 0; 
      int index = 0; 
      while ((b = i.read()) != -1) 
      { 
       responseArray[index] = (byte) b; 
       index++; 
      } 
      i.close(); 

      // Close the connection 
      Log.d(TAG, "[MMS Sender] Disconnecting ..."); 
      connection.disconnect(); 
     } 
     else 
     { 
      return false; 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 


    return false;