我想使用本地代理从我的 Android 设备发送一个 http 请求。在详细说明我的问题之前,我想让你知道这在我尝试过的每台设备上都运行良好,除了在 Android 4.0.4 下运行的 Xperia arc S 和 Xperia T。我在两台设备上都有数据连接,而且都在工作!
我使用了一个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 的情况下做同样的事情?