user3820753 user3820753 - 3 months ago 31
Android Question

java.net.UnknownHostException using AsyncHttpClient

I am trying to POST a request to server side from android client side, using AsyncHttpClient :

For now i just want to check whether the response is coming back or not , so i have not implemented anything to parse request parameters at server side and have just returned some json as response.

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


I get the toast from onStart when the app runs, but then in logcat i get this exception :
java.net.UnknownHostException: Unable to resolve host "sairav.pythonanywhere.comhttp": No address associated with hostname

Note : I have already reset the Wifi connection and run again (on searching other similar questions) , but the problem persists.

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)

Answer

One thing i can see in PAAPI.post call is calling getabsoluteUrl which adds base url with the url passed in.

That is definitely wrong. It will result sairav.pythonanywhere.comhttp://sairav.pythonanywhere.com