I need to make GET-request to a url consisting JSON-data and try to use Loopj library, but it returnes nothing. I tried to find examples on the Internet but they turned out, maybe, to be obsolete (onSuccess method had different parameters). I tried to adapt my code to that example and what I got:
String AllData=""; AsyncHttpClient client = new AsyncHttpClient();
client.get("wantedUrl", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
for (byte aResponseBody : responseBody) {
AllData += aResponseBody;
}
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
Toast.makeText(getApplicationContext(),"We got an error",Toast.LENGTH_SHORT).show();
}
});
"wantedUrl" is the url with json, but "AllData" remains empty. How to fix it?
You're getting AsyncHttpResponseHandler callback where you're in need of JsonHttpResponseHandler. Use below code to get the things right on track.
String AllData=""; AsyncHttpClient client = new AsyncHttpClient();
client.get("wantedUrl", new JsonHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
//Here response will be received in form of JSONArray
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
//Here response will be received in form of JSONObject
}
#Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
Toast.makeText(getApplicationContext(), "We got an error", Toast.LENGTH_SHORT).show();
}
});
Related
how to send post request in android
I use async http clint library
I use this methode for get and I need this for post
client.get(getString(R.string.server2) + "culture_types", new TextHttpResponseHandler() {
#Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
#Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
}
}
});
private static AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
params.put("param1", "Test"); // Put parameter name
client.post(Url, params, new TextHttpResponseHandler(){
#Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
}
#Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
}
}
});
I need to send all images stored in a file. Now am sending one image on button click by specifying its name. But how to send all images on button click saved in some folder
public class UploadImagesToServer {
public static void postImage(String ImageLink){
RequestParams params = new RequestParams();
try {
params.put("file", new File(ImageLink));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
AsyncHttpClient mNewCaller = new AsyncHttpClient();
mNewCaller.get(Constants.MediaUpload, params, new JsonHttpResponseHandler() {
#Override
public void onStart() {
super.onStart();
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
Log.e("response page", response.toString());
}
#Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
super.onSuccess(statusCode, headers, responseString);
Log.e("frag", responseString + " " + statusCode);
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray responseString) {
super.onSuccess(statusCode, headers, responseString);
Log.e("frag", responseString + " " + statusCode);
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.e("fail 3", statusCode + "" + errorResponse);
}
#Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
super.onFailure(statusCode, headers, responseString, throwable);
Log.e("fail 3", statusCode + "" + responseString);
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.e("fail 3", statusCode + "" + errorResponse);
}
});
}
}
the above code is for sending an image
continueBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String filePath = Environment.getExternalStorageDirectory()
.getAbsolutePath() + File.separator + "/Omoto Images/background image.jpg";
UploadImagesToServer.postImage(filePath);
}
});
this is for calling postImage method
Best process for uploading multiple images.
Your image uploading code already implemented you need to just change flow.
You could store your image paths in a list (or something similar) and save the
list (it may be shared preference or sqlite).
As you finish uploading a picture, remove it from that list and continue to the next one, and so on upto your list is empty.
When uploading, if the internet connection drops it will not affect you,
you will always have save the list of images that are still to be uploaded.
For get all images path:
You need to scan directory and store image path and before uploading check path file is exist or not because sometime image may be removed or move.
I'm trying to access Google's geocode API using Android-Async-http library in my app. Here is my request and below is response:
���������������UMo�0��WX>Ӫ i{���UJ�$�J�
mߔ%�===�������B��R��}���#�4�.�d�{��$�b��D��V<��<�����w*y�q���y5����o� �����
Here is success callback method of AsyncHttpResponseHandler.
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
if (statusCode == HttpStatus.SC_OK) {
try {
String response = new String(responseBody);
//String response = new String(responseBody, "UTF-8"); //this is also giving junk reponse
Log.v("SUCCESS RESPONSE", response);
networkCallback.onSuccess(response);
}catch (Exception e){
e.printStackTrace();
}
}
}
Why am i getting the response as junk? How do I get proper response?
Try this :
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
super.onSuccess(statusCode, headers, response);
MyLog.log(TAG, response.toString());
// if is Jsonobjec
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
super.onSuccess(statusCode, headers, response);
MyLog.log(TAG, response.toString());
// if is JsonArray
}
Or you want get response string:
#Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
super.onSuccess(statusCode, headers, responseString);
}
Used
compile 'com.loopj.android:android-async-http:1.4.9'
instead of 1.4.5
I am using android-async-http library from Loopj, onFailure not getting called in my code, I have tried all three methods..
W/JsonHttpRH﹕ onFailure(int, Header[], String, Throwable) was not overriden, but callback was received
private void get() {
//Request parameters
RequestParams params = new RequestParams();
params.put("key", API_KEY);
MyRESTClient.get("/posts/", null, new JsonHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
//some code
}
#Override
public void onProgress(long bytesWritten, long totalSize) {
mLoaderProgress.setText((long) 100 * bytesWritten / totalSize + "Loading...");
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
super.onFailure(statusCode, headers, throwable, errorResponse);
Log.d("Error----> ", ""+statusCode+" ------ "+ errorResponse);
}
});
}
Below is correct method to 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);
}
You need to override correct method . Some of the onFailure are :
1.public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse)
2.public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse)
3.public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable)
Choose the proper onFailure to avoid the error.
Use this for Header
import cz.msebera.android.httpclient.Header;
I have the following mental model that for my android app, all authentication failures should be handled in a single place and the login screen should interrupt and be flashed on top of the view stack to allow the user to reauthenticate.
Not sure how to do this with AsyncHTTPClient.
I do not want to keep handling the 401 and 404 requests for every single GET and POST request that I make. Sounds like a lot of repeated code. So for google and CNN, for example, if I am not authenticated, I don't want to repeat the code to handle the 401 not authenticated code for both CNN and google because it's the same exact code.
Thank you!
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.google.com", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(response);
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
error.printStackTrace(System.out);if 401, TELL USER he has to log in again
}
});
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.cnn.com", new AsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(response);
}
#Override
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable
error)
{
error.printStackTrace(System.out);if 401, TELL USER he has to log in again
}
});
create your own subclass for AsyncHttpResponseHandler and implement the onFailure alone; Add all your failure handling logic in there.
Use this subclass for your client.get instead of AsyncHttpResponseHandler, just implementing the onSuccess.
client.get("http://www.cnn.com", new MyAsyncHttpResponseHandler() {
#Override
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
System.out.println(response);
}
// onFailure handled by common code in MyAsyncHttpResponseHandler
}