java.net.UnknownHostException 使用 AsyncHttpClient

java.net.UnknownHostException using AsyncHttpClient

我正在尝试 POST 从 android 客户端向服务器端发送请求,使用 AsyncHttpClient :

现在我只想检查响应是否返回,所以我没有在服务器端执行任何解析请求参数的操作,只是返回了一些 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 ::

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;
}
}

当应用程序 运行 时,我从 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)

我在 PAAPI.post 调用中看到的一件事是调用 getabsoluteUrl,它添加基数 url 和传入的 url。

这绝对是错误的。结果是 sairav.pythonanywhere.comhttp://sairav.pythonanywhere.com

post()

中删除 getAbsoluteUrl() 方法

原因 结果是 sairav.pythonanywhere.comhttp://sairav.pythonanywhere.com