我正在尝试使用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;
}
}
当应用程序运行时,我从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