【问题标题】:Android : How can I cache the tweets that I’ve got using the Fabric SDK for offline mode?Android:如何缓存我使用 Fabric SDK 离线模式获得的推文?
【发布时间】:2017-03-26 06:18:25
【问题描述】:

我开始使用 Fabric SDK,我一直在尝试将推文缓存到 android 设备,当用户离线并且在用户查看推文列表后没有网络连接时。

我正在使用 client.getSearchService().tweets(....) 方法来检索我的推文,并且我得到了一个 com.twitter.sdk.android.core.models.Tweet 对象列表的响应,就像在 Retrofit 中一样。

我正在尝试将这些推文写入我的内部存储文件,但由于推文模型不可序列化,因此我无法存储它。有没有办法从 Fabric SDK 获得 JSON 响应?我在 iOS Fabric SDK 上寻找类似于client.URLRequestWithMethod(...) 的服务。

任何帮助将不胜感激。提前致谢。

【问题讨论】:

  • 你不能用谷歌gson序列化它吗?
  • 或者将Tweet对象转换为json并存储在SQLIte数据库中?
  • 这有什么更新吗?

标签: android twitter-fabric


【解决方案1】:

我最终解决了这个问题,使用自定义服务和客户端进行搜索以获取 JSON 响应,而不是使用改造的映射。

例如,对于搜索服务,

interface CustomSearchService {
    @GET("/1.1/search/tweets.json")
    void tweets(@Query("q") String var1, @EncodedQuery("geocode") Geocode var2, @Query("lang") String var3, @Query("locale") String var4, @Query("result_type") String var5, @Query("count") Integer var6, @Query("until") String var7, @Query("since_id") Long var8, @Query("max_id") Long var9, @Query("include_entities") Boolean var10, Callback<Response> var11);
}

static class SearchApiClient extends TwitterApiClient {

    public SearchApiClient(Session session) {
        super(session);
    }

    public CustomSearchService getCustomSearchService() {
        return getService(CustomSearchService.class);
    }
}

并通过获取数据,

apiClient.getCustomSearchService().tweets(searchParam.toString(), null, null, null, null, TWEET_COUNT, null,null, null, true,
        new Callback<Response>() {

 @Override
 public void success(Result<Response> result) {

       //DO SOMETHING WITH THE JSON RESPONSE HERE
       // InputStream stream = result.data.getBody().in(); //<-- This contains the data

   }
}

【讨论】:

    【解决方案2】:

    Twitter 支持自定义 OkHttp 客户端,查看他们的示例应用:

    https://github.com/twitter/twitter-kit-android/blob/master/samples/app/src/main/java/com/example/app/SampleApplication.java

    我附上了相关的sn-p:

        final TwitterAuthConfig authConfig = new TwitterAuthConfig(BuildConfig.CONSUMER_KEY,
                BuildConfig.CONSUMER_SECRET);
    
        final Fabric fabric = new Fabric.Builder(this)
                .kits(new Twitter(authConfig))
                .logger(new DefaultLogger(Log.DEBUG))
                .debuggable(true)
                .build();
    
        Fabric.with(fabric);
    
        final HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
        final OkHttpClient customClient = new OkHttpClient.Builder()
                .addInterceptor(loggingInterceptor).build();
    
        final TwitterSession activeSession = TwitterCore.getInstance()
                .getSessionManager().getActiveSession();
    
        final TwitterApiClient customApiClient;
        if (activeSession != null) {
            customApiClient = new TwitterApiClient(activeSession, customClient);
            TwitterCore.getInstance().addApiClient(activeSession, customApiClient);
        } else {
            customApiClient = new TwitterApiClient(customClient);
            TwitterCore.getInstance().addGuestApiClient(customApiClient);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-11
      • 2016-06-29
      • 2018-11-13
      • 1970-01-01
      • 2015-08-11
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      相关资源
      最近更新 更多