retrofit

How to get JSON ARRAY and JSON object response from retrofit response?

余生长醉 提交于 2019-12-23 02:45:33
问题 I have worked with file upload using retrofit. It works fine. But how do handle the retrofit success response. and How do i create serialization model class for below Json array and Json object. { "result": [{ "fileId": 869, "status": 1, "pcData": { "id": 652, "filename": "IMG_20161122_175344.jpg", "filepath": "uploads\/peoplecaddie\/files\/1743_1481109145_IMG_20161122_175344.jpg" } }] } Here is My call method Call<ServerResponse> call = service.upload("817b6ce98fd759e7f148b948246df6c1", map,

CURL -u request with Retrofit

隐身守侯 提交于 2019-12-23 01:29:24
问题 I am using Retrofit (not the new one) in Android to do some OAuth authorization. I am past the first step where I get a code to use, now the next thing in the specifications is to do this: curl -u : http://example.com/admin/oauth/token -d 'grant_type=authorization_code&code=' I have never done curl requests, I don't know what to do, especially with Retrofit and it's interfaces. I took a look at this How to make a CURL request with retrofit? but this guy doesnt have a -d thing 回答1: The -d

NetworkOnMainThreadException in concatMap() with Retrofit 2 and RxJava

泄露秘密 提交于 2019-12-22 11:37:07
问题 In my app I'm trying to use RxJava to react when the user search for a new movie and retrieve movie data from a webservice. In order to do this I thought to use concatMap to concatenate the action of getting the new movie query to requesting the movie to the webservice. Doing this I get a NetworkOnMainThread exception and I don't understand the reason.. createSearchViewObservable(searchView) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .concatMap(new Func1<String,

Retrofit and persistent cookie store

Deadly 提交于 2019-12-22 11:09:26
问题 What is the most simple way to implement persistent cookie store on retrofit? Now I am using this: cookieManager = new CookieManager(); cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL); CookieHandler.setDefault(cookieManager); but I need cookies to be saved and restored any other time. 回答1: You would need to build your own CookieStore implementation. The implementation would be up to you. You could choose to persistent the data into SharedPreference, flat file, JSON, XML ... It would

How do I handle response errors using with Retrofit and RxJava/RxAndroid?

我与影子孤独终老i 提交于 2019-12-22 10:59:46
问题 I am having trouble working out how to handle a response error with retrofit and RxAndroid. onError() gets called if there is a network error or the like but I need to be able to get the response to check if there was an authentication error. Instead what I get is a token with a null String and I can't find out why. What is the best way to go about this? This is my RxAndroid call at the moment. Client.getInstance().getService() .getToken(usernameET.getText().toString(), passwordET.getText()

Retrofit 源码解析:一款基于 OkHttp 的网络请求库

ぐ巨炮叔叔 提交于 2019-12-22 10:06:12
Retrofit 可以说和 OkHttp 是亲兄弟了,它们都是由 Square 公司推出的网络请求库,并且 Retrofit 实际上是基于 OkHttp 实现的,它在 OkHttp 现有功能的基础上进行了封装,支持通过注解进行网络请求参数的配置,同时对数据返回后的解析、序列化进行了统一的包装,甚至在近期引入了对协程对支持。 今天就让我们一起来看看 Retrofit 是如何在 OkHttp 这样一个已经固定的框架的基础上,优雅的进行封装并拓展功能的。 本篇源码解析基于 Retrofit v2.7.0 个人博客: https://blog.N0tExpectErr0r.cn 小专栏: https://xiaozhuanlan.com/N0tExpectErr0r 基本使用 我们首先来看看 Retrofit 的基本使用,来对它有个大致的了解。 首先,我们可以构建一个如下的请求 Service 类,它里面对各个请求的方法及参数通过注解进行了标注: public interface GitHubService { @GET ( "users/{user}/repos" ) Call < List < Repo > > listRepos ( @Path ( "user" ) String user ) ; } 之后,我们可以构建一个 Retrofit 对象,并通过 Retrofit

Unable to parse error in Retrofit 2

喜你入骨 提交于 2019-12-22 05:26:07
问题 I am using Retrofit 2 and I am getting a null pointer exception at this line: RetrofitClient.APIError error = RetrofitClient.ErrorUtils.parseError(response, retrofit); The error is null. More details: This is the format that the API returns the error in: { "error": { "message": "Incorrect credentials", "statusCode": 401 } } Here is my login Callback code: new Callback<LoginResponse>() { @Override public void onResponse(Response<LoginResponse> response, Retrofit retrofit) { if (listener !=

How to emit items from a list with delay in RxJava?

别说谁变了你拦得住时间么 提交于 2019-12-22 04:46:10
问题 I'm using Retrofit to get bookmarks from REST API: public interface BookmarkService { @GET("/bookmarks") Observable<List<Bookmark>> bookmarks(); } Now I would like to emit each item from this list with delay. I did something similar to this in Java, but onCompleted is never triggered. private Observable<Bookmark> getBookmarks() { return getBookmarkService().bookmarks() .flatMap(new Func1<List<Bookmark>, Observable<Bookmark>>() { @Override public Observable<Bookmark> call(List<Bookmark>

Using OkHttp client via OKClient on Google App Engine throws a “java.lang.NoClassDefFoundError: java.net.ProxySelector” is a restricted class error

自作多情 提交于 2019-12-22 04:04:15
问题 I am trying to use OKHTTP (version 2.4.0) along retrofit (1.9.0) on google app engine (1.9.22). Here is the how i use it: OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setConnectTimeout(COMPOSER_MODULE_CONNECTION_TIMEOUT, TimeUnit.SECONDS); okHttpClient.setReadTimeout(COMPOSER_MODULE_SOCKET_TIMEOUT, TimeUnit.SECONDS); RestAdapter restAdapter = new RestAdapter.Builder() .setLogLevel(RestAdapter.LogLevel.FULL) .setConverter(new JacksonConverter()) .setEndpoint(ENDPOINT_PATH)

how to use retrofit 2 to send file and other params together

无人久伴 提交于 2019-12-21 20:17:24
问题 I am looking for an example how could I send file and other params together to server. I have to send server JSON which { "title": "title", "file": "uploaded file instance", "location": { "lat": 48.8583, "lng": 2.29232, "place": "Eiffel Tower" } } How could I create Retrofit to handle this case? If file is a string I know how to handle this. If file is File object I have no idea how to do this. 回答1: Use gson and create a model class for the location. Add the following dependencies to your