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;
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 am trying to upload image using Params.
Android code:
POST Data to Server
RequestParams params = new RequestParams();
params.put("item_name", "Name of item");
params.put("item_image", encodedImage);
MyRestClient.post(MainActivity.this, "item",params, new JsonHttpResponseHandler(){
#Override
public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
JSONArray jArr = response;
super.onSuccess(statusCode, headers, response);
}
#Override
public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
String responseFromAPI = responseString;
super.onFailure(statusCode, headers, responseString, throwable);
}
#Override
public void onSuccess(int statusCode, Header[] headers, String responseString) {
String responseStr = responseString;
super.onSuccess(statusCode, headers, responseString);
}
#Override
public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
JSONObject jObj = response;
super.onSuccess(statusCode, headers, response);
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
JSONObject jOBj = errorResponse;
super.onFailure(statusCode, headers, throwable, errorResponse);
}
#Override
public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) {
JSONArray jArr = errorResponse;
super.onFailure(statusCode, headers, throwable, errorResponse);
}
});
Bitmap Image Encode
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
Slim Framework Code:
$app->post('/image', function ($request, $response) {
$input = $request->getParsedBody();
$uploaded_image = $input['image_image'];
$path = "/..../uploads/"."img-".date("Y-m-d-H-m-s").".jpg";
if (file_put_contents($path, base64_decode($uploaded_image)) != false)
{
$sql = "INSERT INTO item (item_name, item_image) VALUES (:restaurant_name, :restaurant_image)";
$sth = $this->db->prepare($sql);
$sth->bindParam("item_name", $input['item_name']);
$sth->bindParam("item_image", $input['item_image']);
$sth->execute();
$input['id'] = $this->db->lastInsertId();
}
return $this->response->withJson($input);
});
Problem:
The photo should be uploaded as per the code and my understanding. It is not uploading the image to the desired folder.
Am I doing things correctly or have I missed something?
<?php
$app->post('/image', function ($request, $response) {
$files = $request->getUploadedFiles();
$file = $files['image_image']; // uploaded file
$parameters = $request->getParams(); // Other POST params
$path = "/..../uploads/"."img-".date("Y-m-d-H-m-s").".jpg";
if ($file->getError() === UPLOAD_ERR_OK) {
$file->moveTo($path); // Save file
// DB interactions here...
$sql = "INSERT INTO item (item_name, item_image) VALUES (:restaurant_name, :restaurant_image)";
$sth = $this->db->prepare($sql);
$sth->bindParam("item_name", $input['item_name']);
$sth->bindParam("item_image", $input['item_image']);
// if statement is executed successfully, return id of the last inserted restaraunt
if ($sth->execute()) {
return $response->withJson($this->db->lastInsertId());
} else {
// else throw exception - Slim will return 500 error
throw new \Exception('Failed to persist restaraunt');
}
} else {
throw new \Exception('File upload error');
}
});
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 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();
}
});