简体   繁体   English

Android:如何从Volley OnRequest StringRequest方法返回JSONObject

[英]Android : how to return JSONObject from Volley OnRequest StringRequest method

i need to pass the response string by assigning value to the string data to another method out side the OnResponse() scpoe so that i can return a JSONObject from it by calling that method but it always returns null 我需要通过将字符串数据的值分配给OnResponse()scpoe之外的另一个方法来传递响应字符串,以便我可以通过调用该方法从中返回JSONObject,但它始终返回null

all i need is to get JSONObject from Volley stringrequest as the response is a xml " 我需要的只是从Volley stringrequest中获取JSONObject,因为响应是xml“

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">........</string>

Here is my code 这是我的代码

static String data;

private  void driverByIdStringRequest(int ID,final Context context){
        RequestQueue queue = VolleySingleton.getInstance(context.getApplicationContext()).getRequestQueue();
        // Request a string response from the provided URL.
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url+ID,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        response = response.replaceAll("<?xml version=\"1.0\" encoding=\"utf-8\"?>", "");
                        response = response.replaceAll("<string xmlns=\"http://tempuri.org/\">", "");
                        response = response.replaceAll("</string>", "");
                        String data = response.substring(40);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error : ", error.toString());
            }
        });
        VolleySingleton.getInstance(context).addToRequestQueue(stringRequest);
    }


public JSONObject GetDriverById(int ID,Context context){
    driverByIdStringRequest(ID, context);
    JSONObject json = JSONObject(data);
    return json;
}

The best maintainable way to do it is first to parse you XML with a Parser. 最好的可维护方法是首先使用解析器解析XML。

Just follow this tutorial on the official doc. 只需按照官方文档上的本教程进行即可。

Once you got your string you just have to do 拿到琴弦后,您只需要做

JSONObject mainObject = new JSONObject(Your_Sring_data);

(Android studio will prompt you to put the correct try/catch around the JSON creation.) (Android Studio会提示您围绕JSON创建进行正确的尝试/捕获。)

If the String response from your StringRequest always has the following format: 如果来自StringRequest的String响应始终具有以下格式:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">........</string>

in which ........ is the JSONObject you want to get, I suggest that you process the String reponse like the following way: 其中........是要获取的JSONObject ,我建议您按照以下方式处理String响应:

...
String jsonString = stringResponse;

jsonString = jsonString.replace("<?xml version=\"1.0\" encoding=\"utf-8\"?>"," ");
jsonString = jsonString.replace("<string xmlns=\"tempuri.org/\">"," ");
jsonString = jsonString.replace("</string>"," ");
try {
    JSONObject jsonObject = new JSONObject(jsonString);
} catch (JSONException e) {
    e.printStackTrace();
}
...

Back to your code, data of course NULL because JSONObject json = JSONObject(data); 回到您的代码, data当然为NULL,因为JSONObject json = JSONObject(data); called before onResponse inside the Request called. onResponse的请求内的onResponse之前调用。 I suggest that you refer to my answer at the following question for your issue: 我建议您参考以下问题的答案:

Android: How to return async JSONObject from method using Volley? Android:如何使用Volley从方法返回异步JSONObject?

Hope this helps! 希望这可以帮助!

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

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