【问题标题】:Using square retrofit library to make http requests使用方形改造库发出 http 请求
【发布时间】:2014-08-02 20:50:31
【问题描述】:

我使用的是loopj async http library to make http requests,但在研究about android networking library 后发现,retrofit 比 volley 更好,后者是最快且最可靠的网络库。

我计划更改我的代码以适合retrofit..

以前,我用这个方法制作HTTP requests

AsyncHttpClient AHC = new AsyncHttpClient();
        RequestParams param = new RequestParams();
        param.put("arg1", arg1);
        param.put("arg2", arg2);
        AHC.post("http://xxxxx.xxx.xxxx.xxxx", param,
                new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(String content) {
                        // TODO Auto-generated method stub

我得到的内容是json type.

我读到改造默认使用 gson.. 我认为这真的更快。

以前我用这种方式填充我的内部数据库:

JSONArray jArray = new JSONArray(content);
for (int i = 0; i < jArray.length(); i++) {
    JSONObject json = jArray.getJSONObject(i);
    TD.CreatePostsTable(
            json.getString("id")}

这些方法在改造中会变成怎样?

非常感谢大家!

【问题讨论】:

  • 从服务器返回数据时不使用 POJO 吗?您只需遍历 json 并保存在您的数据库中?
  • @LeonardoFerrari 是的,我为什么要使用 POJO?
  • 这是一种很好的做法,可以让您的代码更容易被其他人阅读。简单地遍历 JSON 是很难维护和混淆的!
  • @LeonardoFerrari 感谢您提供的信息和您的帮助。我会在了解方法后立即接受您的回答。
  • 没问题。很高兴我能帮上忙!如果您有任何问题,请询问:)

标签: android httprequest gson retrofit


【解决方案1】:

你的发帖方式:

@POST("/users/login")
    YOUR_RETURN_TYPE loginUser(@Field("arg1") String arg1,
                   @Field("arg2") String arg1);

以及改造配置:

new RestAdapter.Builder()
                .setEndpoint("http://your_url")
                .setLogLevel(RestAdapter.LogLevel.FULL).build();

我推荐这种方法与 Retrofit 一起使用,使用 Singleton:

private Map<String, Object> restInstances = new HashMap<String, Object>();
public <T> T getRescClient(Class<T> clazz) {
    T client = null;

    if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
        return client;
    }

    client = restAdapter.create(clazz);
    restInstances.put(clazz.getCanonicalName(), client);
    return client;
}

然后调用这个:

 restApiProvider.getRestClient(UserService.class).your_method()

希望对你有帮助!

【讨论】:

    【解决方案2】:

    看看这个实现:https://github.com/UweTrottmann/tmdb-java

    Tmdb 类构建不同的服务。例如,MovieServices 具有:

    @GET("/movie/{id}")
    Movie summary(
            @Path("id") int tmdbId
    );
    

    此方法将调用 /movie/{id} 方法到返回 JSON 的 REST 服务器。该方法将返回一个 Movie 对象,其中属性与 JSON 键具有相同的名称。

    public class Movie extends MediaBase implements TraktEntity {
    private static final long serialVersionUID = -1543214252495012419L;
    
    @SerializedName("tmdb_id") public int tmdbId;
    public Integer plays;
    @SerializedName("in_collection") public Boolean inCollection;
    public Date released;
    public String trailer;
    public Integer runtime;
    public String tagline;
    public String overview;
    public String certification;
    public Boolean watched;
    
    }
    

    【讨论】:

    • 可能是第一次使用很困难。但他们,永远是平等的。
    猜你喜欢
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多