I'm using this library to request from my web services. It didn't response at all in onSuccess and onFailure sometimes (these are the only two methods I overrides). I tested under 1.4.4 and 1.4.5 (android-async-http-1.4.5-20131110.125018-1, this one is better, but still encounter the problem sometimes). I'm sure it's not the network problem, because my ios app never encounter this problem from the same web services. And I can get response when I refresh immediately after this problem occurs.
Here is my code:
In requester.java
public class Requester
{
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
AsyncHttpClient client = newClient();
client.get(getAbsoluteUrl(url), params, responseHandler);
}
public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
AsyncHttpClient client = newClient();
client.post(getAbsoluteUrl(url), params, responseHandler);
}
private static String getAbsoluteUrl(String relativeUrl) {
return Settings.BASE_URL + relativeUrl;
}
private static AsyncHttpClient newClient()
{
AsyncHttpClient client = new AsyncHttpClient();
client.setMaxRetriesAndTimeout(Settings.HTTP_TIMEOUT,Settings.HTTP_RETRIES);
return client;
}
}
In my activity who's making http request:
Requester.get(urlRequest, null, new JsonHttpResponseHandler()
{
#Override
public void onSuccess(int statusCode, org.apache.http.Header[] headers, org.json.JSONArray objects)
{
Logger.logDebug(TAG, "request success for " + " " + objects.length() + " objects");
}
#Override
public void onFailure(int statusCode, org.apache.http.Header[] headers, java.lang.Throwable throwable, org.json.JSONArray errorResponse)
{
Logger.logError(TAG,"Failed to request");
}
});
I'm using the similar source in a few projects. But all have the same problem. I don't know it's the problem of my code or the android-async-http library. Can anybody help? Thanks.
By the way, I'm normally making 3 requests at the same time by using the same method as the above mentioned source code but with different url.
I confirmed that the problem is related to the multiple requests. My solution is to replace with another library: Volley library. Problem solved!
I encounter this problem as well and upon further investigation I realized that if the json response received is not in proper json format or if the response from the server contains any extra characters you will not get a callback method being called, although the post or get would have been processed the callbacks will not be triggered. Check your response from the server to make sure.
Related
Hi I've been using this free GIT library for some time now and I just experience a very very weird problem
I have a URL (web API) that returns a JSON formatted data.
Yes, it exist on browser I tried all main browser out there and it is showing.
The problem is when I used it on my AsyncHttp LoopJ it returns a 404 error, which fires
onFailure(int statusCode, Header[] headers, byte[] responseBytes, Throwable throwable)
status code is 404 and throwable message is "not found".
I've test other URL on my other project and it works only not this specific URL. That guarantees that it works on other URL.
I cant say that the API has a problem, since it shows JSON data on web browser.
I clean the project rebuild nothing works.
Please anyone had the same struggle here.
I check the manifest, check internet connection all good. Like I said it works on other URL, except this one.
PS. I like the simplicity of this library thats why I dont want to switch to other similar libraries out there.
What im using is this class
public class TerminalClient {
private static AsyncHttpClient client = new AsyncHttpClient();
public static AsyncHttpClient syncHttpClient = new SyncHttpClient();
public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
getClient().setTimeout(1000 * 10);
getClient().get((url), params, responseHandler);
}
public static void asyncPost(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
getClient().setTimeout(1000 * 10);
getClient().post((url), params, responseHandler);
}
public static void post(String url, RequestParams params, BaseJsonHttpResponseHandler baseJsonHttpResponseHandler) {
getClient().setTimeout(1000 * 10);
getClient().post((url), params, baseJsonHttpResponseHandler);
}
private static AsyncHttpClient getClient() {
if (Looper.myLooper() == null)
return syncHttpClient;
return client;
}
}
I just switch to Post to Get. That did it, pretty weird. Yep.
This can be a reference for future guys who will bump in this kind of problem.
-Cheers
Hello guys I'm new to restful service.
My friend created REST backend with Spring.
When I post this URL --> http://smartcar.tobaconsulting.com:9999/api/v1/login with postman or angularjs http.post, it's fine.
You guys can check it out in postman by including this body
{ "username":"alvin", "password":"alvin" }
and set content type to JSON (application/json).
But when I code to Android, why it's not working and return 500 error code.
My friend said that I'm not including header. I'm using loopj http library http://loopj.com/. Here is my android code
RequestParams params = new RequestParams();
params.put("username", username);
params.put("password", password);
AsyncHttpClient client = new AsyncHttpClient();
client.post("http://smartcar.tobaconsulting.com:9999/api/v1/login", params, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
Log.d(TAG, String.format("status code: %d", statusCode));
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Log.d(TAG, String.format("status code: %d", statusCode));
}
});
Please help me guys. I trying to solve this problem for hours and haven't find any clue.
Thanks
i checked your URL no error in server response
Access-Control-Allow-Credentials →true
Access-Control-Allow-Methods →GET, POST, PUT, DELETE
Access-Control-Allow-Origin →chrome-extension://mkhojklkhkdaghjjfdnphfphiaiohkef
Access-Control-Expose-Headers →X-AUTH-TOKEN
Content-Length →0
Date →Thu, 07 Apr 2016 08:35:26 GMT
Server →Apache-Coyote/1.1
X-AUTH-TOKEN
→eyJ1c2VybmFtZSI6ImFsdmluIiwiZW1haWwiOiJhbHZpbkBhbHZpbi5jb20iLCJleHBpcmVzIjoxNDYwODgyMTI2MDc4LCJvd25lcmlkIjo2LCJteVJvbGVzIjpbIlVTRVIiLCJBRE1JTiJdfQ==.M9/71trIPbnXxCh7avSWBK42UDXUxWYXZrNOlHhO7iQ=
I would personally suggest using Volley lib for android, there is some useful method inside volley and google strongly recommended that
Transmitting Network Data Using Volley
I'm using LoopJ AndroidAsyncHttp to post async Http calls from my android app. When i specify some RequestParams, the call keep failing, with a statusCode 415(Unsupported Media Type). As soon as i remove the requestParams the call goes through, without any error.
final AsyncHttpClient client;
String url = "http://someURL.com/SomeUserGUID/Profile/Statistics/Setup";
client = new AsyncHttpClient();
final RequestParams params = new RequestParams();
params.put("FirstClub", "false");
params.put("FairwayHit", "false");
Header[] headers = {
new BasicHeader("Accept-Language", Locale.getDefault().toString())
,new BasicHeader("Authorization", ApplicationObject.companyBasicAuthString)
,new BasicHeader("Accept", "application/json")
};
client.setTimeout(60000);
client.post( ProfileActivity.this,url, headers, params, "application/json", new AsyncHttpResponseHandler() {
#Override
public void onStart() {
//Do something before start
super.onStart();
}
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
//some code
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
//some code
}
});
My goal is for the URL to look like this: String url = http://someURL.com/SomeUserGUID/Profile/Statistics/Setup?FirstClub=false& FairwayHit=false
I would pref not to hard code the URL into one single string(This method works btw), because i have some calls that would create a really long URL string.
So how can i achieve a successful post call, with params, using AndroidAsyncHttp?
415 Unsupported Media Type
The server is refusing to service the request because the entity of the request is in a format not supported
by the requested resource for the requested method.
You're sending the Content-Type header (in your post call) as "application/json", but the params you've added are not JSON. This is probably why the error occurs.
I'm using loopj to talk to a rest server which has different HTTP response codes for specific successful responses (i.e 200, 202). The AsyncHttpResponseHandler has an onSuccess(String content) callback but nothing that returns the HTTP response code. How can I get this?
Try
#Override
public void onSuccess(int statusCode, String content) {
}
Here is the code that I have used for Async Http requests using loopj.
AsyncHttpClient loopjClient = new AsyncHttpClient();
PersistentCookieStore loopjCookieStore = new PersistentCookieStore(this);
loopjClient.setCookieStore(loopjCookieStore);
RequestParams loopjParams = new RequestParams();
loopjParams.put("username", username);
loopjParams.put("email", emailID);
loopjParams.put("password", password);
Log.d(TAG, "RRRRRRlll");
loopjClient.post("http://www.example.com/", loopjParams, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int k,String response) {
super.onSuccess(k, response);
Log.d(TAG, "RRRRRR "+ response );
}
});
And my server response string is just the following with out the HTML tags
{"AUTHENTICATION":{"SUCCESS":false,"USERNAME":"unregistered","USERID":0},"ALERTBOX":{"SHOW":true,"MESSAGE":"Message : Username or email ID already registered. Please choose a different one."}}
But loopj doesnt print the above string in onSuccess().
Can you tell how to get the string or do I have to fall back to default Android http library ?
Thanks in advance.
EDIT
Instead of 'example'(my site) if I replace it with 'facebook' I just get the following string as response with a 200 response code <?xml version="1.0" encoding="utf-8"?>
Since the result of AUTHENTICATION failed, I'm wondering if the status code that your server return is not 200. You can override onFailure method to deal with this situation.
#Override
public void onFailure(Throwable e) {
Log.d(TAG, e.toString());
}
Edit
Another guess, have you added the following line in your Manifest.xml?
<uses-permission android:name="android.permission.INTERNET" />
Edit Again
I saw your edit, and it seems that you have an buggy LogCat as #shailendra-rajawat mentioned in comment, since LogCat printed only the first line. Try Toast instead:
#Override
public void onSuccess(int k, String response) {
Toast.makeText(getBaseContext(), response, 1).show();
}
I found this post for configuring your LogCat, and maybe you can give it a try. (Mine is 5000 FYI)