json - Android 同步连接到 REST/JSON - Volley、VolleyPlus 还是 Retrofit?

标签 json rest android-volley synchronous retrofit

我使用 Volley 为抽屉导航的不同部分(总共 7 个)使用不同的类(POJO、解析器、JSON 请求)开发了我的所有应用程序,但是在一切启动并运行后,我意识到它需要使用同步/阻塞截击通常不会做的请求。所有 Volley 请求都是异步的,这意味着有时我单击按钮或抽屉项,但它不会显示任何结果,因为异步请求未完成。

我做了一些研究,发现了几个选择:

我很难找到有关前 2 个选项的教程或示例,用于发送带有参数(X-Auth-Token、Content-Type、Accept application/json 等)的 GET/POST 请求并转向 Retrofit意味着我必须扔掉所有已经编写的代码并从头开始。

你的建议是什么?此用例有任何 Volley/VolleyPlus 示例或教程吗?转向 Retrofit 值得重新进行吗?

更新:

我开始使用 Retrofit 并替换了我的一些 Volley 调用。这是完美的!它完全符合我的要求,而且代码比 Volley 还要少。到目前为止,我还没有进行自动解析,只是获取 JSON 格式的原始 HTTP 响应并将其发送到我以前的解析器类,但最终当我了解 POJO 和 GSON 时,我也会将它们替换。我必须在同步调用的主要事件上禁用严格模式,并添加 try/catch 来处理一些错误。以下是其中一种方法的改造代码:

    RestAdapter.Builder builder = new RestAdapter.Builder()
            .setEndpoint(neutronURL)
            .setLogLevel(RestAdapter.LogLevel.FULL)
            .setClient(new OkClient(new OkHttpClient()));
    builder.setRequestInterceptor(new RequestInterceptor() {
        @Override
        public void intercept(RequestFacade request) {
            request.addHeader("X-Auth-Token", authToken);
        }
    });
    RestAdapter adapter = builder.build();
    SecurityAPI api = adapter.create(SecurityAPI.class);
    if (security == null){
        pDialog.setMessage("Contacting Server...");
        pDialog.show();
    }

    try {
        Response result = api.getSecSync();
        security = getRawJSON(result);
    } catch (RetrofitError e) {
        Log.d("Retrofit Error", e.toString());
        if (e.toString().contains("Unauthorized")){
            tokenExpiredAlert();
        }
        if (offline==0 && e.toString().contains("Unable to resolve host")){
            offlineAlert();
        }
    }

以及它调用的接口(interface):

public interface SecurityAPI {
@Headers({
        "Accept: application/json",
        "Content-Type: application/json; charset=utf-8"
})
@GET("/v2.0/security-groups")
//void getSecurityContent(Callback<Response> callback);
Response getSecSync();

}

更新2:

关于 VolleyPlus,我将如何通过这段代码使用它?我尝试过,但没有成功,出现了一些关于抽象类的错误:

     JsonObjectRequest getRequest = new JsonObjectRequest(Request.Method.GET, novaURL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("Nova on Response", response.toString());
                    setNovaJSON(response.toString());
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d("Nova on Error", "Error: " + error.getMessage());
                    setNovaJSON(error.toString());
                }
            }
    ) {
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("X-Auth-Token", authToken);
            params.put("Accept", "application/json");
            params.put("Content-Type", "application/json; charset=utf-8");
            return params;
        }

    };


    queue = VolleySingleton.getInstance(this).getRequestQueue();
    //VolleySingleton.getInstance(this).addToRequestQueue(getRequest);
    queue.add(getRequest);

最佳答案

这可能无法完全回答您的问题,但我只是分享我的观点。

作为一家初创公司的首席 Android 开发人员,我从 Volley 开始(2013 年底),但遇到了问题。我在一周内就转向了 Retrofit。

同步与异步以回调的形式出现。如果存在回调,Retrofit 知道异步运行它,否则它会等待返回值。

关于json - Android 同步连接到 REST/JSON - Volley、VolleyPlus 还是 Retrofit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28042952/

相关文章:

javascript - 获取JSON IP + 国家代码

javascript - 我无法将带有嵌套元素的 json 字符串解析为 jquery

C# 排序 JSON 字符串键

java - 为基于 Spring 的 REST API 编写 Junit 测试用例

android - 如何处理一个巨大的 JSON 数组?

android - 有时,Volley 无法从服务器返回响应

android - 找不到符号 Volley

r - 解析 JSON 数据在隔离一行但不使用 apply 迭代数据帧 R 的行时有效

php - 如何使用 PHP Stargate 客户端将数据插入 Hbase 表

android - Volley 图书馆的麻烦