retrofit2

java.lang.NoClassDefFoundError: okhttp3.OkHttpClient$Builder

回眸只為那壹抹淺笑 提交于 2019-11-27 03:33:00
问题 In my project, I have used OkHttp(version 2.5.0) as my network request library. But few days ago, I tried Retrofit2 as part of my app's network request library, as you know, the new Retrofit2 library dependence on OkHttp(version 3.2.0).So here comes the problem that when my app runs on android phone whose android version under android 5.0 always causes crash(larger version works well and causes no crash), the crash stack information shows as below: 04-15 21:28:51.185 17238-17238/com.comingx

How can I return String or JSONObject from asynchronous callback using Retrofit?

三世轮回 提交于 2019-11-27 03:30:20
For example, calling api.getUserName(userId, new Callback<String>() {...}); cause: retrofit.RetrofitError: retrofit.converter.ConversionException: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 I think I must disable gson parsing into POJOs but can't figure out how to do it. I figured it out. It's embarrassing but it was very simple... Temporary solution may be like this: public void success(Response response, Response ignored) { TypedInput body = response.getBody(); try { BufferedReader reader = new

Retrofit post using Firebase

↘锁芯ラ 提交于 2019-11-27 03:23:26
问题 Please any one please help. This is My API method @POST("/user/new.json") Call createUser(@Body User user); This is my call in MainActivity Retrofit retrofit=new Retrofit.Builder().baseUrl("https://XXXXXX.firebaseio.com").addConverterFactory(GsonConverterFactory.create()).build(); Api api=retrofit.create(Api.class); User user=new User(1,"Sam"); Call<User> call=api.createUser(user); call.enqueue(new Callback<User>() { @Override public void onResponse(Call<User> call, Response<User> response) {

Retrofit 2.0 how to get deserialised error response.body

江枫思渺然 提交于 2019-11-27 03:02:20
I'm using Retrofit 2.0.0-beta1 . In tests i have an alternate scenario and expect error HTTP 400 I would like to have retrofit.Response<MyError> response but response.body() == null MyError is not deserialised - i see it only here response.errorBody().string() but it doesn't give me MyError as object Saif Bechan I currently use a very easy implementation, which does not require to use converters or special classes. The code I use is the following: public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { DialogHelper.dismiss(); if (response.isSuccessful()) { // Do your

Android Dagger2 + OkHttp + Retrofit dependency cycle error

旧街凉风 提交于 2019-11-27 01:53:35
问题 Hey there I am using Dagger2 , Retrofit and OkHttp and I am facing dependency cycle issue. When providing OkHttp : @Provides @ApplicationScope OkHttpClient provideOkHttpClient(TokenAuthenticator auth,Dispatcher dispatcher){ return new OkHttpClient.Builder() .connectTimeout(Constants.CONNECT_TIMEOUT, TimeUnit.SECONDS) .readTimeout(Constants.READ_TIMEOUT,TimeUnit.SECONDS) .writeTimeout(Constants.WRITE_TIMEOUT,TimeUnit.SECONDS) .authenticator(auth) .dispatcher(dispatcher) .build(); } When

Retrofit Post Parameter

允我心安 提交于 2019-11-27 00:51:41
问题 I am implementing login feature and for that using Post request but i am getting error saying "retrofit.RetrofitError: com.squareup.okhttp.internal.http.HttpMethod.METHODS" Below is my code import java.util.HashMap; import java.util.Map; import retrofit.Callback; import retrofit.http.*; //Myapi.java import java.util.HashMap; import java.util.Map; import retrofit.Callback; import retrofit.http.*; public interface MyApi { /* LOGIN */ @POST("/api/0.01/oauth2/access_token/") // your login

Set dynamic base url using Retrofit 2.0 and Dagger 2

旧城冷巷雨未停 提交于 2019-11-27 00:22:08
I'm trying to perform a login action using Retrofit 2.0 using Dagger 2 Here's how I set up Retrofit dependency @Provides @Singleton Retrofit provideRetrofit(Gson gson, OkHttpClient client) { Retrofit retrofit = new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson) .client(client) .baseUrl(application.getUrl()) .build(); return retrofit; } Here's the API interface. interface LoginAPI { @GET(relative_path) Call<Boolean> logMe(); } I have three different base urls users can log into. So I can't set a static url while setting up Retrofit dependency. I created a setUrl() and

Custom converter for Retrofit 2

落爺英雄遲暮 提交于 2019-11-26 22:33:08
I have to handle a dynamic JSON responses. Before, I was using classes and annotations as follows: public class ChatResponse { @SerializedName("status") private int status; @SerializedName("error") private String error; @SerializedName("response") private Talk response; public int getStatus() { return status; } public String getError() { return error; } public Talk getResponse() { return response; } } When the status is 1 (success) the onResponse is fired and I can get a ChatResponse object. But, when the status is 0, the response is false in the JSON representation and it fails ( onFailure is

Dagger + Retrofit dynamic URL

假如想象 提交于 2019-11-26 21:05:16
问题 PROBLEM I need to call API from domains entered by USER and I need to edit my Retrofit singleton before the call accordingly to the inserted data. Is there a way to "reset" my singleton, forcing it to recreate? or Is there a way to update my baseUrl with my data (maybe in Interceptor?) just before call? CODE Singletons @Provides @Singleton Retrofit provideRetrofit(SharedPreferences prefs) { String apiUrl = "https://%1s%2s"; apiUrl = String.format(apiUrl, prefs.getString(ACCOUNT_SUBDOMAIN,

ProtocolException: Expected ':status' header not present

做~自己de王妃 提交于 2019-11-26 20:40:05
问题 Retrofit network calls fails with a Protocol Exception suddenly in a working app. The app was working till yesterday and today all the network calls fails. The calls works fine with HTTP but fails with HTTPS. Here is the logs, java.net.ProtocolException: Expected ':status' header not present 10-18 14:59:01.103 30746-30746/? W/System.err: at okhttp3.internal.http.Http2xStream.readHttp2HeadersList(Http2xStream.java:262) 10-18 14:59:01.103 30746-30746/? W/System.err: at okhttp3.internal.http