retrofit

Retrofit 2.0 - How to get response body for 400 Bad Request error?

ぃ、小莉子 提交于 2019-12-20 11:09:03
问题 So when I make a POST API call to my server, I get a 400 Bad Request error with JSON response. {  "userMessage": "Blah", "internalMessage": "Bad Request blah blah", "errorCode": 1 } I call it by Call.enqueue(new Callback<ResponseBody>() { @Override public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { //AA } @Override public void onFailure(Call<ResponseBody> call, Throwable t) { //BB } } However the problem is that once I get the response, onFailure() is invoke so

Retrofit 2 HTTP method annotation is required (e.g., @GET, @POST, etc.)

十年热恋 提交于 2019-12-20 10:39:41
问题 What's wrong with my Retrofit configuration? I'm having this error when I'm adding Basic Authentication with my OkHttpClient but when I used the default client without Interceptor it's working. Or is there something wrong with my Gradle Dependencies..? E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalArgumentException : HTTP method annotation is required (e.g., @GET, @POST, etc.). for method APIService.getRegAccrDetails at retrofit.Utils.methodError(Utils.java:177) at retrofit.Utils

How to make retrofit API call using ViewModel and LiveData

两盒软妹~` 提交于 2019-12-20 10:28:25
问题 this is the first time I'm trying to implement MVVM architecture, and I'm a bit confused about the correct way to make an API call. Currently, I'm just trying to make a simple query from the IGDB API, and output the name of the first item in a log. My activity is setup as follow: public class PopularGamesActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popular_games);

How to post HTTPS request using Retrofit?

时光总嘲笑我的痴心妄想 提交于 2019-12-20 10:09:36
问题 I build a restAdapter by this: return new RestAdapter.Builder() .setEndpoint("https://www.xyz.com/") .build() .create(SafeUserApi.class); then in the SafeUserApi.class (which is an interface) I have: public interface SafeUserApi { @POST("/api/userlogin") void getUserLogin(@Body UserserLogin userLogin, Callback<LoginResult> cb); } But it does not work. Did I miss something? I tried with Postman and it works. But in my device it doesn't. I have these four library imported: compile files('libs

Retrofit 2 - Elegant way of adding headers in the api level

懵懂的女人 提交于 2019-12-20 09:21:13
问题 My Retrofit 2 ( 2.0.2 currently) client needs to add custom headers to requests. I'm using an Interceptor to add these headers to all requests: OkHttpClient httpClient = new OkHttpClient(); httpClient.networkInterceptors().add(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { final Request request = chain.request().newBuilder() .addHeader("CUSTOM_HEADER_NAME_1", "CUSTOM_HEADER_VALUE_1") .addHeader("CUSTOM_HEADER_NAME_2", "CUSTOM_HEADER_VALUE_2") ...

How to get response body to retrofit exception?

廉价感情. 提交于 2019-12-20 09:14:09
问题 I am trying to connect to rest service via retrofit in android application. I am getting responses. But when there is some error response from the service, conversion exception occurs and now I want to do some actions based on the response body. But I am getting response body as NULL. But retrofit log has a error message. Why is this happening. D/Reftofit log(24856): OkHttp-Received-Millis: 1397527055676 D/Reftofit log(24856): OkHttp-Response-Source: NETWORK 200 D/Reftofit log(24856): OkHttp

Android OkHttp, refresh expired token

安稳与你 提交于 2019-12-20 08:39:02
问题 Scenario : I am using OkHttp / Retrofit to access a web service: multiple HTTP requests are sent out at the same time. At some point the auth token expires, and multiple requests will get a 401 response. Issue : In my first implementation I use an interceptor (here simplified) and each thread tries to refresh the token. This leads to a mess. public class SignedRequestInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request =

I'm getting an error on my response from Retrofit Expected BEGIN_ARRAY but was STRING at line 1 column 1 path $

让人想犯罪 __ 提交于 2019-12-20 05:52:19
问题 I've looked at several answers, I'm missing something. Also is there a way to see the data prior to Retrofit handling it? Here's the code extends Activity implements Callback<List<MemberPOJO>> { @Inject MembersInterface memberInterfaceService; @Override public void onFailure(Exception ex) { setProgressBarIndeterminateVisibility(false); String retrofitError = "Error: " + ex.getMessage(); Log.e("LoadActivity", retrofitError); } @Override public void onSuccess(List<MemberPOJO> result) {

Retrofit with String response

亡梦爱人 提交于 2019-12-19 18:21:08
问题 I want a POST call on an URL, and as response I just get a String "ok" or "no".. So I have here my interface like this: public interface registerAPI { @FormUrlEncoded @POST("addDevice.php") Call<String> insertUser( @Field("name") String devicename, @Field("username") String regid); } So I just want to give the POST method the two parameters, and I want a back a String. In a PHP script on the server, there is something like this: <?php if(...) echo "ok"; else echo "no"; So I call on my Android

java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

大城市里の小女人 提交于 2019-12-19 17:43:22
问题 I am using Retro Fit to connect to API online. But I am getting this error while trying to parse the returned data. retrofit.RetrofitError: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 The data being returned is in This format and model for data is also given below: iGPlaceApi.getStreams(ITEMS_PER_PAGE, pageNumber * ITEMS_PER_PAGE, new Callback<List<mGooglePlacesApiResponse>>() { @Override public void