I have fragment and Common class which inside it used retrofit callback..I have Connect_and_get class.It sends request to server and gets information.I must use this information in my fragment.. But I can't return result onResponse.How can I do it..(Response is coming well from server)
Please see my code
public class Connect_and_Get {
private int size;
private OkHttpClient.Builder httpClient;
private ApiService client;
private Call<Response> call;
private MyPreference myPreference;
String a[] = {"secret"};
String b[] = {"secret"};
public int Connect_and_Get() {
Requests request;
request = new Requests("tasks.list", new params(20, 0, a, b, "", "", "", "", ""));
httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder().addHeader("Authorization", "Bearer " + "secret").build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.client(httpClient.build())
.build();
client = retrofit.create(ApiService.class);
call = client.getDocument(request);
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
size = response.body().getResult().getList().size();
}
#Override
public void onFailure(Call<Response> call, Throwable t) {
}
});
//retruning information
return size;
}
}
and result from common class coming 0;Because it doesn't wait my response so it is returning 0;
In fragment
Connect_and_Get a = new Connect_and_Get();
int getting = a.Connect_and_Get();
Log.d("mylog", "result:"+String.valueOf(getting));
Declare an interface like this
public interface ResponseListener {
public int onResponse(int size);
}
and use below code in your activity
public class MainActivity extends AppCompatActivity implements ResponseListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Connect_and_Get().Connect_and_Get(this);
}
#Override
public int onResponse(int size) {
// to do
return 0;
}
}
modify your connect class like this
public class Connect_and_Get {
public int Connect_and_Get(ResponseListener responseListener) {
// as it was
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
size = response.body().getResult().getList().size();
responseListener.onResponse(size);
}
#Override
public void onFailure(Call<Response> call, Throwable t) {
}
});
}
}
You need to check whether your response is successful or not.
Check the code below.
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
if(response.isSuccessful()){
//enter code here
} else {
//Error Message
}
}
#Override
public void onFailure(Call<Response> call, Throwable t) {
Log.e("Log", "Error -> "+t.getLocalizedMessage());
}
});
You can use an event bus like (rxbus,otto, etc..) to post events across your app when the response from the api ready to use .
Retrofit callback sample code:
call.enqueue(new Callback<Response>() {
#Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response) {
Bus.getInstance.post(Event)
}
#Override
public void onFailure(Call<Response> call, Throwable t) {
}
});
Fragment sample:
#Override
protected void onCreate(Bundle savedInstanceState) {
Bus.getInstance.register(this)
}
#Subscribe
public void onCallDone(Event response) {
//enter code here
}
Related
I want to run changesHasBeenSaved if saveChanges function gives me true value.
How can I get boolean or string or integer, any value, when Retrofit finish?
Is it possible?
I need a function similar this:
public boolean saveChanges()
{
Boolean output = false;
RequestAPI requestAPI = Requests.getRetrofit();
Call<Object> jsonObjectCall = requestAPI.readAllCategoeies();
jsonObjectCall.enqueue(new Callback<Object>() {
#Override
public void onResponse(Call<Object> call, Response<Object> response) {
if(response.body() != null) {
output = true;
}
}
#Override
public void onFailure(Call<Object> call, Throwable t) {
call.cancel();
}
});
return output;
}
You should change method return type boolean to void as retrofit work asynchronous.
You should pass the listener/callback to get the status in callback.
First create callback interface like below
public interface ApiCallback {
void onResponse(boolean success);
}
Here how saveChanges will look like
public void saveChanges(final ApiCallback callback)
{
RequestAPI requestAPI = Requests.getRetrofit();
Call<Object> jsonObjectCall = requestAPI.readAllCategoeies();
jsonObjectCall.enqueue(new Callback<Object>() {
#Override
public void onResponse(Call<Object> call, Response<Object> response) {
callback.onResponse(response.body() != null);
}
#Override
public void onFailure(Call<Object> call, Throwable t) {
call.cancel();
callback.onResponse(false);
}
});
}
Then you need to call saveChanges method like below
saveChanges(new ApiCallback () {
public void onResponse(boolean success){
if(success){
// do something
} else{
// do something
}
}
});
jsonObjectCall.enqueue(new Callback<Object>() {
#Override
public void onResponse(Call<Object> call, Response<Object> response) {
//when the response is successful
if(response.isSuccessful()) {
output = true;
// display response as string
Log.i(TAG, "post submitted to API." + response.body().toString());
//Object user1 = response.body(); maps response to the modal class
}
}
#Override
public void onFailure(Call<Object> call, Throwable t) {
call.cancel();
}
})
Change you call back to like the above , included comments for clearity about usage .
Hope this helps.
I am working with Retrofit (JSON Parsing Android) and I am new with this I am so confused to set the response with the Base Adapter.
This is my Example Class
public class Example {
#SerializedName("Totalrecord")
#Expose
private List<Totalrecord> totalrecord = null;
#SerializedName("Table1")
#Expose
private List<Table1> table1 = null;
#SerializedName("Table2")
#Expose
private List<Table2> table2 = null;
public List<Totalrecord> getTotalrecord() {
return totalrecord;
}
public void setTotalrecord(List<Totalrecord> totalrecord) {
this.totalrecord = totalrecord;
}
public List<Table1> getTable1() {
return table1;
}
public void setTable1(List<Table1> table1) {
this.table1 = table1;
}
public List<Table2> getTable2() {
return table2;
}
public void setTable2(List<Table2> table2) {
this.table2 = table2;
}
}
Here I am trying to retrieve Response in ArrayList so that I can set it to Adapter
ApiService api = RetroClient.getApiService();
Call<Example> call = api.getMyJson(38, 109);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
dialog.dismiss();
if (response.isSuccessful()) {
// Response_list = response.body().get...
adapter_view = new Adapter_view(Response_list, MainActivity.this);
listView.setAdapter(adapter_view);
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
dialog.dismiss();
System.out.println("failed");
}
});
But I am confused to set The type of ArrayList and also Type of Callback<> so that I can get the response properly and set ArrayList to the Adapter.
try following if you want to set List<Totalrecord> in your adapter.
ApiService api = RetroClient.getApiService();
Call<Example> call = api.getMyJson(38, 109);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
dialog.dismiss();
if (response.isSuccessful()) {
Response_list.clear();
Response_list.addAll(call.getTotalrecord());
adapter_view = new Adapter_view(Response_list, MainActivity.this);
listView.setAdapter(adapter_view);
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
dialog.dismiss();
System.out.println("failed");
}
});
List<Table1> mList=new ArrayList();
ApiService api = RetroClient.getApiService();
Call<Example> call = api.getMyJson(38, 109);
call.enqueue(new Callback<Example>() {
#Override
public void onResponse(Call<Example> call, Response<Example> response) {
dialog.dismiss();
if (response.body.isSuccessful()) {
mList=response.body.getTable1();
adapter_view = new Adapter_view(mList,MainActivity.this);
listView.setAdapter(adapter_view);
}
}
#Override
public void onFailure(Call<Example> call, Throwable t) {
dialog.dismiss();
System.out.println("failed");
}
});
com.squareup.retrofit2:retrofit:2.3.0
My steps:
Start async request: getCompaniesList()
Wait success response
Start another async request: getCatalogsList()
Wait success response
Do some another code
Here snippet in my activity:
RestClient restClient = RestClientFactory.getRestClient();
Call<List<Company>> companyList = restClient.getCompaniesList(filters);
companyList.enqueue(new Callback<List<Company>>() {
#Override
public void onResponse(Call<List<Company>> call, Response<List<Company>> response) {
if (response.isSuccessful()) {
RestClient restClient = RestClientFactory.getRestClient();
Call<List<Catalog>> catalogList = restClient.getCatalogsList(filters);
catalogList.enqueue(new Callback<List<Catalog>>() {
#Override
public void onResponse(Call<List<Catalog>> call, Response<List<Catalog>> response) {
if (response.isSuccessful()) {
// HERE SOME NEED CODE!!!
}
}
#Override
public void onFailure(Call<List<Catalog>> call, Throwable throwable) {
}
});
}
}
#Override
public void onFailure(Call<List<Company>> call, Throwable throwable) {
}
});
I think this structure is not very nice. Тoo many nested items. As result code is more complicated.
Question: Has any alternative to this structure?
You can either take time to learn Rx RXjava2
or you can split up your code like this
RestClient restClient = RestClientFactory.getRestClient();
Call<List<Company>> companyList = restClient.getCompaniesList(filters);
companyList.enqueue(getCompanyListCallback());
private Callback<List<Company>> getCompanyListCallback() {
return new Callback<List<Company>>() {
#Override
public void onResponse(Call<List<Company>> call, Response<List<Company>> response) {
if (response.isSuccessful()) {
RestClient restClient = RestClientFactory.getRestClient();
Call<List<Catalog>> catalogList = restClient.getCatalogsList(filters);
catalogList.enqueue(getCatalogsListCallback());
}
}
#Override
public void onFailure(Call<List<Company>> call, Throwable throwable) {
}
};
}
private Callback<List<Catalog>> getCatalogsListCallback() {
return new Callback<List<Catalog>>() {
#Override
public void onResponse(Call<List<Catalog>> call, Response<List<Catalog>> response) {
if (response.isSuccessful()) {
// HERE SOME NEED CODE!!!
}
}
#Override
public void onFailure(Call<List<Catalog>> call, Throwable throwable) {
}
};
}
I am Using Retrofit v2.0.2. I am not able to get the JSON ResponseI am getting error like this okhttp3.ResponseBody$1#501040a,Please any one help me in this case.
This is Interface:
public interface IdeaInterface {
/*Get zoontype for registeration*/
#GET("api/user/get_zone")
Call<ResponseBody> display();
}
calling retrofit method here.
public void Get_Zoontype()
{
reg_progressbar.setVisibility(View.VISIBLE);
/* RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(Constandapi.ROOT_URL) //Setting the Root URL
.build();*/
// Retrofitnew Retrofit.Builder();
Retrofit adapter = new Retrofit.Builder()
.baseUrl(Constandapi.ROOT_URL)
.build();
IdeaInterface report = adapter.create(IdeaInterface.class);
Call<ResponseBody> call = report.display();
call.enqueue(new Callback<ResponseBody>() {
#Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
Log.d("reponcevalues","**** "+response.toString());
}
#Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
}
});
This is my Base URL:
public class Constandapi {
public static final String ROOT_URL = "http://vehiclerescue.in/ideadarpan_beta/";
}
my build.gradle which has retrofit library
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
Thanks in Advance.
Retrofit code, in It am having Upload Model class.
Call<Uploads> uploadsCall = service.profilePicUpload(body, id);
uploadsCall.enqueue(new Callback<Uploads>() {
#Override
public void onResponse(Call<Uploads> call, Response<Uploads> response) {
response.body();
if (response.isSuccessful()) {
String s = response.body().getMsg();
Communicate.showToast(MyAccountActivity.this, s);
ImageHelper.getImage(MyAccountActivity.this, AppController.ProficPic + profpic, profilePic,
R.drawable.ic_user, R.drawable.ic_user, new ImageHelper.Callback() {
#Override
public void Success() {
runOnUiThread(new Runnable() {
#Override
public void run() {
profpicProgress.setVisibility(View.GONE);
}
});
}
#Override
public void Error() {
runOnUiThread(new Runnable() {
#Override
public void run() {
profpicProgress.setVisibility(View.GONE);
}
});
}
});
} else {
Communicate.showToast(MyAccountActivity.this, "please try again");
profpicProgress.setVisibility(View.GONE);
}
}
#Override
public void onFailure(Call<Uploads> call, Throwable t) {
profpicProgress.setVisibility(View.GONE);
Communicate.showToast(MyAccountActivity.this, "error");
Log.e(AppController.TAG, "onFailure: " + t.getLocalizedMessage());
Log.e(AppController.TAG, "onFailure: " + t.getMessage());
t.printStackTrace();
}
});
Here i have a model class, I am getting msg and stud_id as json objects
public class Uploads {
#SerializedName("msg")
#Expose
private String msg;
#SerializedName("stud_id")
#Expose
private String stud_id;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getStud_id() {
return stud_id;
}
public void setStud_id(String stud_id) {
this.stud_id = stud_id;
}
}
Hop this will help you
I'm working on Retrofit 2.0 (which is awesome) to handle API responses.
All works well when API answers with success, I return the converted object wished from the json response
Here an example of request:
ServiceAPI.getUser(new Callback<User>() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body(); // user is my POJO
}
#Override
public void onFailure(Call<User> call, final Throwable t) {
Log.e(TAG, "Error: " + t.getMessage())
}
});
When response is done, I return the POJO (which is my main purpose), not the json to parse in order to avoid boilerplate.
So here my code to handle this :
// the interface to handle calls
protected interface ServiceAPI {
#GET("/user/{userId}")
Call<User> getUser(#Path("userId") String userId);
}
// the GSON part for converting data
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory())
.setDateFormat("yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSS'Z'")
.create();
// an interceptor to log requests responses
OkHttpClient okClient = new OkHttpClient.Builder()
.addInterceptor(new LogJsonInterceptor())
.build();
// the retrofit builder
retrofit = new Retrofit.Builder()
.baseUrl(mBaseUrl)
.client(okClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
// the interceptor to log requests
public static class LogJsonInterceptor implements Interceptor {
#Override
public okhttp3.Response intercept(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
okhttp3.Response response = chain.proceed(request);
String rawJson = response.body().string();
Log.d(TAG, rawJson);
// Re-create the response before returning it because body can be read only once
return response.newBuilder().body(ResponseBody.create(response.body().contentType(), rawJson)).build();
}
}
// here the magic to handle json response and get data from "data" json key
public static class ItemTypeAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
private RestError error;
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("data")) {
jsonElement = jsonObject.get("data");
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
// here the method to call from an activity for example to get an User
public static void getUser(final String userId, final Callback<User> callback) {
serviceAPI.getUser(userId).enqueue(new Callback<User>() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
callback.onResponse(call, response);
}
#Override
public void onFailure(Call<User> call, Throwable t) {
callback.onFailure(call, t);
}
});
}
// here the call from the Activity
ServiceAPI.getUser(new Callback<User>() {
#Override
public void onResponse(Call<User> call, Response<User> response) {
User user = response.body();
}
#Override
public void onFailure(Call<User> call, final Throwable t) {
Log.e(TAG, "Error: " + t.getMessage())
}
});
// when API answers with success (code 200 from headers)
{
"data":
{
"id":1,
"name":"myname",
"email":"myemail"
}
}
So all works well here because I get response from "data" and convert the response into my POJO
PROBLEM:
But when API answers with an error (also with code 200 from headers), I get this:
{
"error":
{
"code":200
"type":"OAuth_exception",
"message":"You need an access token to get an user",
}
}
The problem is that the retrofit "response" is nevertheless successfull and the errorBody is null
So here, I would to convert this into a RestError POJO (below) and send it inside the call when calling getUser method
public class RestError {
private int code;
private String message;
private String type;
public RestError(int code, String message, String type) {
this.code = code;
this.message = message;
this.type = type;
}
}
Any idea to fix that?
UPDATE:
I added this in the ItemTypeAdatperFactory
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("data")) {
jsonElement = jsonObject.get("data");
} else if (jsonObject.has("error")) {
jsonElement = jsonObject.get("error");
TypeAdapter<RestError> restErrorTypeAdapter = gson.getAdapter(RestError.class);
RestError error = restErrorTypeAdapter.fromJsonTree(jsonElement);
return (T) error;
}
}
return delegate.fromJsonTree(jsonElement);
}
And I created a custom Callback like this:
public abstract static class CustomCallback<T> implements Callback<T> {
public abstract void onError(RestError error);
public abstract void onSuccess(T body);
#Override
public void onResponse(Call<T> call, Response<T> response) {
if(response.body() instanceof RestError) {
onError((RestError) response.body());
} else {
onSuccess(response.body());
}
}
#Override
public void onFailure(Call<T> call, Throwable t) {
Log.e(TAG, t.toString());
}
}
So now how the call is:
ServiceAPI.getUser(new ServiceAPI.CustomCallback<User>() {
#Override
public void onError(RestError error) {
Log.e(TAG, error.toString());
}
#Override
public void onSuccess(User body) {
Log.d(TAG, body.toString());
User user = body;
}
#Override
public void onFailure(Call<User> call, Throwable t) {
Log.e(TAG, t.toString());
}
});
So now when an error occurs I get it from onError(), otherwise onSuccess() and seems to do the job
What do you think about?
You can extend all your POJOs from some Response class, which will contain field RestError error. Then you can make some isSuccessful method, which will check is error field null or not and handle it properly.
In my view, you can customize your ItemTypeAdapterFactory like that to check if the reponse is your data or it is an error
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has("data")) {
jsonElement = jsonObject.get("data");
} else if(jsonObject.has("error"){
jsonElement = jsonObject.get("error");
}
}
//TODO: need to handle your parsing here (to data or to error)
return delegate.fromJsonTree(jsonElement);
}