Android koush ion library catch 401 error - android

I'm using Koush Ion on Android to handle net operations.
I'd like to add a common handler to handle 401 unauthorized error.
How i can do it?
thank you

Ion.with(this)
.load("http://google.com")
.asString()
.withResponse()
.setCallback(new FutureCallback<Response<String>>() {
#Override
public void onCompleted(Exception e, Response<String> result) {
if(result.getHeaders().code()){
// ............
}
}
});
goodluck!

Related

How to load an image/jpeg with retrofit and archictecture component?

I'm using archictecture Android Architecture Components. I want to load an image from my api.
this is the request on postman :
!https://ibb.co/WPJjpFm
I' am actually using retrofit but i don't known how to transform Responsebody to file.
#GET("image/{filename}")
Call<ResponseBody> getImage(#Path("filename") String filename);
public MutableLiveData<File> retriveImageTest(String filename) {
MutableLiveData data = new MutableLiveData<>();
executor.execute(() -> {
imageWebService.getImage(filename).enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// How to deal with responsebody ?
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
});
return data;
}
What' s the best way to do it without breaking Android Architecture Components ?
Thanks in advance.
It is better to use Image loading libraries like Glide ,Picasso or Fresco to load images:
Using Glide, add this to your app gradle file
implementation 'com.github.bumptech.glide:glide:4.9.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0'
Then ,
Glide.with(this).load(imageUrl).into(imageView);
For more info : Glide docs

How to Send JSON data and another file in the same POST request?

My post request should have 2 thing 1st json and 2nd file
I am trying it on postman it is happening but I don’t understand how to do in android
This is how I am hitting URL using post man
What is the best for this to be done from android side
use multipart request for android post. you can use android networking like this.
AndroidNetworking.upload(url)
.addMultipartFile("image",file)
.addMultipartParameter("data",jsondata.tostring)
.setTag("uploadTest")
.setPriority(Priority.HIGH)
.build()
.setUploadProgressListener(new UploadProgressListener() {
#Override
public void onProgress(long bytesUploaded, long totalBytes) {
// do anything with progress
}
})
.getAsJSONObject(new JSONObjectRequestListener() {
#Override
public void onResponse(JSONObject response) {
// do anything with response
}
#Override
public void onError(ANError error) {
// handle error
}
});

how to handle empty response body in android fast networking library?

how to handle an empty response body in android fast networking library?
I know in retrofit we have void, but how to handle in fast networking.
POST request, request content type JSON, empty response body with Http code.
retrofit equivalent:
#POST("/path/to/get")
Call<Void> getMyData(/* your args here */);
my code is below
#Override
public Single<Response> checkPhoneNumberAvailabilityApiCall(CheckPhoneNumberAvailabilityRequest checkPhoneNumberAvailabilityRequest) {
return Rx2AndroidNetworking.post(ApiEndPoint.getEndPoint(ApiEndPoint.ENDPOINTS.CHECK_PHONE_AVAILABLE_API))
.addHeaders(getHeaders(GENERAL))
.addJSONObjectBody(convertToJSON(checkPhoneNumberAvailabilityRequest))
.build()
.getObjectSingle(Response.class);
}
I have no experience in Retrofit yet, but hope it helps
AndroidNetworking.post(YOUR_ENDPOINT)
.addHeaders(YOUR_HEADER)
.addJSONObjectBody(YOUR_JSONOBJECTBODY)
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
#Override
public void onResponse(JSONObject response) {
if (response.toString().isEmpty()) {
//handle empty body
} else {
//do logic with json value
}
}
#Override
public void onError(ANError anError) {
//print log error code
Log.e("FAN_ERROR", "ERROR CODE : "+ anError.getErrorCode());
//print log error body
Log.e("FAN_ERROR", "ERROR BODY : "+ anError.getErrorBody());
}
});

Callback call with Ion on Intent Service(android)

In my application I want to send my recordings to the web site on backgroud. I've researched and I reached the conclusion that I should use and IntentService.
But this leads me to a problem, i cannot reach the callback of Ion inside the IntentService.
The callback is important because I want to delete the file after finishing the upload.
How can I make this?
This is my code:
Ion.with(this)
.load(ip_address)
.setMultipartParameter("key", key)
.setMultipartFile("audio_file", array_mp3[idx])
.asJsonObject()
.setCallback(new FutureCallback<JsonObject>() {
#Override
public void onCompleted(Exception e, JsonObject result) {
Log.d("BackS", "wifiteste - chega aqui ");
array_mp3[idx].delete();
}
});
I never reach that Log which means that i never delete the file.
Thanks in advance.

Ion Android getHeaders

How do I get a specific header using the Ion network library for Android?
This is what I'm trying:
...
.setCallback(new FutureCallback<Response<String>>() {
#Override
public void onCompleted(Exception e, Response<String> result) {
System.out.println("header: " + result.getHeaders().message("mykey"));
}
});
message(String) sets the HTTP response message. I should make that internal to avoid confusion.
Use
getHeaders().getHeaders().get(String)
It's a bit obtuse at the moment, will need to clean that API up in the next release.

Categories

Resources