Download file using android-async-loopj - android

I am using this code:
/* This is an asynchronusHTTP client*/
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, params,new FileAsyncHttpResponseHandler(file) {
#Override
public void onSuccess(int statusCode, Header[] headers, File response) {
// Do something with the file `response`
}
});
but I got the file which is empty. Documentation says on success it writes result to the file passed but that is not happening.

What your are saying is called URL Encoding which will not give illegal character error as for as I know.. If you think that URL Encoding is the problem the try add this line in your get(String url, RequestParams params,FileAsyncHttpResponseHandler responseHandler)) function.
client.setURLEncodingEnabled(false);

Related

Unable to retrieve JSON body from a AsyncHttpClient Post request

I am trying to get make a post request in Android using AsyncHttpClient using this code:
JSONObject jsonParams=new JSONObject();
try {
jsonParams.put("email", email);
}
catch (Exception e){
}
try {
StringEntity entity = new StringEntity(jsonParams.toString());
AsyncHttpClient client = new AsyncHttpClient();
client.post(getApplicationContext(),URL,entity, "application/json", new AsyncHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {}
});
I am trying to access this data sent in the post request in my Python server. However, whenever I do request.json in my Python server, I get 'None' as response.
I also tried request.get_json(force=True) but getting 404 error code.
I dont really know if the data is being sent by the client APP or I am making a mistake while trying to retrieve it at the server side. Please help me with this issue.
String content = new String(responseBody);
Log.e("SUCCESS_RESP",""+content);

RESTful web service error 500 on android

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

LoopJ AndroidAsyncHttp Can't POST Image With Basic Auth

I am trying to upload an image to a PHP file on a server using the POST method. I have been trying to do this using LoopJ AndroidAsyncHttp with no success. The server also requires a basic auth username and password. So far, I have been able to successfully POST the regular data parameters (These are simple string key-valued pairs like: "name":"joe") and get a response from the server. However, as soon as I try to attach the image to the POST request, the request fails giving me the following errors:
Error Message: null
Error Cause: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
The code that I am using follows the examples given at http://loopj.com/android-async-http/ very closely. Here is the code that I am using:
RequestParams params = new RequestParams();
params.put("name",name);
String path = "/path/to/img";
File myFile = new File(path, "picture.png");
if( myFile.exists() ) {
try {
params.put("picture", myFile);
} catch(FileNotFoundException e) {
Log.d("App","Error Attaching Picture: " + e.toString());
}
} else {
Log.d("App","File DOES NOT exist");
}
String urlString = "url-to-server";
AsyncHttpClient client = new AsyncHttpClient();
client.setBasicAuth("User", "Pass");
client.post(urlString, params, new AsyncHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
super.onSuccess(statusCode, headers, responseBody);
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
super.onFailure(statusCode, headers, responseBody, error);
Log.d("App","Upload Failed!");
Log.d("App","Error Message: " + error.getMessage());
Log.d("App", "Error Cause: " + error.getCause());
}
#Override
public void onStart() {
super.onStart();
}
});
So what am I doing wrong here?
I have also double checked and the file that I am reading to get the image does exist and it does have data in it, so I have ruled that out as a potential cause.
I have been struggling with this issue a little too long now.
Thanks in advance to anyone who can help!
This was a bug in the old 1.4.4 version of AsyncHTTPClient. It can be fixed by updating to the 1.4.8 version. In your build.gradle file under the dependencies section it should look like this:
compile 'com.loopj.android:android-async-http:1.4.8'

LoopJ AndroidAsyncHttp returns code 415 when specifying RequestParams

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.

Android Asynchronous Http Client : #Override onSuccess(int statusCode, Header[] headers, String content)

I'm trying to get cookie from server response (from Headers).
I built library from source : https://github.com/loopj/android-async-http and add it to my project.
Here i try to override metod
client.post(context, link, requestEntity, contentType, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, String content) {
Log.d("headers", String.valueOf(headers));
onSuccess(statusCode, content);
}
});
but get error :
The method onSuccess(int, PreferenceActivity.Header[], String) of type
new AsyncHttpResponseHandler(){} must override or implement a
supertype method
AsyncHttpResponseHandler.java
How I can override this metod or get Headers?
Headers type supposed to be org.apache.http.Header[] instead of Header[]
I have ever faced with this problem. Try to open your gradle (Gradle Scripts), edit your build.gradle(Module:app) by adding:
dependencies {
compile 'com.loopj.android:android-async-http:1.4.9'
}
after your gradle running, try import cz.msebera.android.httpclient.Header;
I hope it will help you :)

Categories

Resources