首页 > 解决方案 > Android Volley 发布请求 - 无法解析响应?

问题描述

出于某种原因,我无法让它工作。如果我在 reqbin 等其他测试站点上尝试,请求应该可以工作并以正确的格式返回数据。

这是安卓代码:

System.out.println("VOLLEY POST");

    String postUrl = "***MY LOGIN URL***";
    RequestQueue requestQueue = Volley.newRequestQueue(this);

    JSONObject postData = new JSONObject();
    try {
        postData.put("email", "test@email.dk");
        postData.put("password", "abc123");

    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, postUrl, postData, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            System.out.println(response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            error.printStackTrace();
        }
    });

    requestQueue.add(jsonObjectRequest);

但是,当我尝试运行它时,出现此错误:

com.android.volley.ParseError: org.json.JSONException: Value <pre of type java.lang.String cannot be converted to JSONObject

当我尝试在像 reqbin 这样的网站上使用相同的参数运行完全相同的请求时,我得到了这个响应(实际上是有效的 JSON):

  {
    "currency": 97,
    "email": "jeppe@mail.dk",
    "id": 1
}

谁能帮我澄清一下?我的 Java 代码中有什么错误吗?我一直在为此挠头..

顺便说一句,我是 Android 编程的新手,我来自 web/iOS 背景。

非常感谢阅读!

标签: androidjsonandroid-volley

解决方案


推荐阅读