Created
April 3, 2018 13:21
-
-
Save MRezaNasirloo/70be5f34166bef60f99b8c93093bf4ad to your computer and use it in GitHub Desktop.
Retrofit parse error body sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mrezanasirloo.retrofiterrorhandling; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.widget.Toast; | |
import com.google.gson.annotations.Expose; | |
import com.google.gson.annotations.SerializedName; | |
import java.io.IOException; | |
import java.lang.annotation.Annotation; | |
import okhttp3.ResponseBody; | |
import retrofit2.Call; | |
import retrofit2.Callback; | |
import retrofit2.Converter; | |
import retrofit2.Response; | |
import retrofit2.Retrofit; | |
import retrofit2.converter.gson.GsonConverterFactory; | |
import retrofit2.http.Body; | |
import retrofit2.http.POST; | |
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = MainActivity.class.getSimpleName(); | |
private Retrofit retrofit; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
retrofit = new Retrofit.Builder() | |
.addConverterFactory(GsonConverterFactory.create()) | |
.baseUrl("https://reqres.in/api/") | |
.build(); | |
ReqresApi api = retrofit.create(ReqresApi.class); | |
api.login(new BodyModel("peter@klaven")) | |
.enqueue(new Callback<ExpectedModel>() { | |
@Override | |
public void onResponse(@NonNull Call<ExpectedModel> call, @NonNull Response<ExpectedModel> response) { | |
Log.d(TAG, "onResponse() called with: call = [" + call + "], response = [" + response + "]"); | |
if (response.isSuccessful()) { | |
//Do what you got to do | |
} else { | |
Converter<ResponseBody, ErrorModel> converter = MainActivity.this.retrofit.responseBodyConverter(ErrorModel.class, new Annotation[0]); | |
ErrorModel errorModel = null; | |
try { | |
errorModel = converter.convert(response.errorBody()); | |
Toast.makeText(MainActivity.this, errorModel.toString(), Toast.LENGTH_SHORT).show(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public void onFailure(@NonNull Call<ExpectedModel> call, @NonNull Throwable t) { | |
Log.d(TAG, "onFailure() called with: call = [" + call + "], t = [" + t + "]"); | |
} | |
}); | |
} | |
class ExpectedModel { | |
} | |
public class ErrorModel { | |
@SerializedName("error") | |
@Expose | |
public String error; | |
@Override | |
public String toString() { | |
return "ErrorModel{" + | |
"error='" + error + '\'' + | |
'}'; | |
} | |
} | |
public class BodyModel { | |
@SerializedName("email") | |
@Expose | |
public String email; | |
public BodyModel(String email) { | |
this.email = email; | |
} | |
} | |
interface ReqresApi { | |
@POST("login") | |
Call<ExpectedModel> login(@Body BodyModel body); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment