简体   繁体   English

如何读取POST凌空的响应?

[英]How to read response from a POST volley?

I'm really new on android and I'm wokring on a login system, I'm using volley to post the data....the problem thatm I'm having is when i try to read the response.... the response looks like this: 我在Android上真的很新,而且我正在使用登录系统,我正在使用凌空来发布数据......我遇到的问题是当我尝试阅读响应时....响应看起来像这样:

{"st":"no","Message":"Error"} { “ST”: “无”, “消息”: “错误”}

I'm trying to access only st or message is there a way to do that ? 我试图只访问st或消息有没有办法做到这一点? I tried doing: 我试过做:

response[i] ----Array type expexted found ' org.json.JSONObject' response [i] ----找到的数组类型'org.json.JSONObject'

 JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, url, params, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) { 
                    Log.e(TAG, "Response: " + response.length());
                    for (int i = 0; i < response.length(); i++) {
                        Log.e(TAG, "Values: " + response);
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace(); 
                }
            });

            Volley.newRequestQueue(this).add(jsonRequest);

You could use 你可以用

response.getString("Message")

to get a message from given JSON 从给定的JSON获取消息

 @Override
 public void onResponse(String response) {
    try {
           JSONObject api_response = new JSONObject(response);
           String message = api_response.getString("Message")
       } catch (JSONException e) {
            e.printStackTrace();           
       }

Its important to catch the json exception just in case the response string cannot be converted to json object. 捕获json异常非常重要,以防响应字符串无法转换为json对象。 Then use the getString() to get the message from the created json object. 然后使用getString()从创建的json对象中获取消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM