java.net.UnknownHostException使用AsyncHttpClient

时间:2016-09-01 18:19:18

标签: java android http asynchttpclient

我正在尝试使用AsyncHttpClient将来自Android客户端的请求发送到服务器端:

现在我只想检查响应是否回来,所以我没有实现任何解析服务器端请求参数的东西,只是返回了一些json作为响应。

 RequestParams params = new RequestParams();
    params.put("key", "value");
    params.put("more", "data");

    PAAPI.post("http://sairav.pythonanywhere.com",params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            // If the response is JSONObject instead of expected JSONArray
            Log.d("Response::",response.toString());
            Toast.makeText(act,response.toString(),Toast.LENGTH_LONG).show();
        }
        @Override
        public void onStart() {
            // called before request is started
            Toast.makeText(act,"Going to make API CALL",Toast.LENGTH_LONG).show();

        }
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            super.onFailure(statusCode, headers, responseString, throwable);
            Log.d("Failed: ", ""+statusCode);
            Log.d("Error : ", "" + throwable);
        }
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {
            // Pull out the first event on the public timeline
            JSONObject firstEvent = null;
            try {
                firstEvent = timeline.getJSONObject(0);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String tweetText = null;
            try {
                tweetText = firstEvent.getString("text");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            // Do something with the response
            Toast.makeText(act,tweetText,Toast.LENGTH_LONG).show();
        }
    });

PAAPI类::

class PAAPI {
protected static final String BASE_URL = "http://sairav.pythonanywhere.com";

private static AsyncHttpClient client = new AsyncHttpClient();

public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.get(getAbsoluteUrl(url), params, responseHandler);
}

public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
    client.post(getAbsoluteUrl(url), params, responseHandler);
}

private static String getAbsoluteUrl(String relativeUrl) {
    return BASE_URL + relativeUrl;
}
}

当app运行时,我从onStart获取吐司,但是在logcat中我得到了这个异常: java.net.UnknownHostException:无法解析主机" sairav.pythonanywhere.comhttp ":没有与主机名关联的地址

注意:我已经重置了Wifi连接并再次运行(在搜索其他类似问题时),但问题仍然存在。

Logcat:

24154-24228/com.example.sairav.moneytor W/System.err: java.net.UnknownHostException: Unable to resolve host "sairav.pythonanywhere.comhttp": No address associated with hostname
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at java.net.InetAddress.lookupHostByName(InetAddress.java:440)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at java.net.InetAddress.getAllByName(InetAddress.java:215)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:44)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.resolveHostname(DefaultClientConnectionOperator.java:259)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:159)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:145)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:131)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:611)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:446)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:860)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at cz.msebera.android.httpclient.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:146)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:177)
09-01 23:32:11.718 24154-24228/com.example.sairav.moneytor W/System.err:     at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:106)

2 个答案:

答案 0 :(得分:2)

我在exec调用中可以看到的一件事就是调用PAAPI.post,它会在传入的网址中添加基本网址。

这绝对是错误的。这将导致 sairav.pythonanywhere.comhttp://sairav.pythonanywhere.com

答案 1 :(得分:2)

getAbsoluteUrl()

中删除post()方法

<强>原因  它将导致sairav.pythonanywhere.comhttp://sairav.pythonanywhere.com