【问题标题】:Volley onResponse android not workVolley onResponse android不起作用
【发布时间】:2017-07-30 20:07:59
【问题描述】:

MainActivity:

RequestQueue requestQueue;
String url = "http://andriodtest.eb2a.com/show.php";
TextView textView;
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.TextView);

requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,
        new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_LONG).show();

                try {
                    JSONArray jsonArray = response.getJSONArray("users");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject respons = jsonArray.getJSONObject(i);
                        String id = respons.getString("id");
                        String info = respons.getString("name");
                        textView.append(id);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

        }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e("VOLLEY", "ERROR");
    }
}

);
requestQueue.add(jsonObjectRequest);

运行应用时没有调用数据 并且文件配置是 100% 工作的。

example

如果设置 Textview="text" 不起作用。

我认为问题是公共无效 onResponse。 重要的请帮帮我

【问题讨论】:

  • 您的 PHP 后端提供的不是 JSON,而是原始 HTML。

标签: java android android-volley jsonresponse


【解决方案1】:

您的代码看起来不错,但是您的服务器的响应似乎没有返回 json 对象,而是返回一个用于设置 cookie 的 javascript 文件(您可以尝试使用 Postman 访问您的 url 以查看我提到的 javascript 响应) .

Volley 期望收到 json 响应,这可能是您的应用当前无法按预期运行的原因。

我可能会尝试改变服务器的响应方式。以下链接提供了一些解决该问题的建议: Why I can't retrieve data from my webserver as json but i can when i test it on localhost

【讨论】:

    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 2021-07-31
    相关资源
    最近更新 更多