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?
你已經知道地址了嗎?看起來有點像你傳遞它作爲一個字符串?這沒有必要。另外,你有沒有試圖擺脫10的領先0? – 2013-02-21 14:59:26
你提到你有一個數據連接是這個WiFi連接或其他什麼?我相信192.168。*。*子網僅用於內部路由IP。所以3g/4g/etc連接不可能工作,但無線網絡連接將會是 – 2013-02-21 15:07:49
是的,我把這個地址作爲一個字符串傳遞。但'setDefaultProxy'接受'HttpHost'實例。我該怎麼辦呢?我不明白你的第二個問題... – Manitoba 2013-02-21 15:08:34