Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:如何从Volley OnRequest StringRequest方法返回JSONObject_Java_Android_Android Volley_Jsonobject - Fatal编程技术网

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

Java Android:如何从Volley OnRequest StringRequest方法返回JSONObject,java,android,android-volley,jsonobject,Java,Android,Android Volley,Jsonobject,我需要通过将字符串数据的值赋给OnResponse()scpoe之外的另一个方法来传递响应字符串,这样我就可以通过调用该方法从中返回JSONObject,但它总是返回null 我只需要从Volley stringrequest中获取JSONObject,因为响应是xml“ ........ 这是我的密码 static String data; private void driverByIdStringRequest(int ID,final Context context){

我需要通过将字符串数据的值赋给OnResponse()scpoe之外的另一个方法来传递响应字符串,这样我就可以通过调用该方法从中返回JSONObject,但它总是返回null

我只需要从Volley stringrequest中获取JSONObject,因为响应是xml“


........
这是我的密码

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;
}
静态字符串数据;
私有void driverbydstringrequest(int-ID,最终上下文){
RequestQueue queue=VolleySingleton.getInstance(context.getApplicationContext()).getRequestQueue();
//从提供的URL请求字符串响应。
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url+ID,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
response=response.replaceAll(“,”);
response=response.replaceAll(“,”);
response=response.replaceAll(“,”);
字符串数据=响应。子字符串(40);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(“Error:,Error.toString());
}
});
getInstance(上下文).addToRequestQueue(stringRequest);
}
公共JSONObject GetDriverById(int ID,上下文){
DriverByDStringRequest(ID、上下文);
JSONObject json=JSONObject(数据);
返回json;
}

最好的可维护方法是首先使用解析器解析XML

只需关注官方文件

一旦你拿到了你的绳子,你只需要

jsonobjectmainobject=新的JSONObject(您的数据);


(Android studio将提示您在JSON创建过程中使用正确的try/catch。)

最好的维护方法是首先使用解析器解析XML

只需关注官方文件

一旦你拿到了你的绳子,你只需要

jsonobjectmainobject=新的JSONObject(您的数据);


(Android studio将提示您在JSON创建过程中使用正确的try/catch。)

如果来自StringRequest的字符串响应始终具有以下格式:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">........</string>
回到你的代码,
data
当然是空的,因为
JSONObject json=JSONObject(data);
在调用请求之前调用了
onResponse
。我建议你参考我对以下问题的回答,了解你的问题:


如果来自StringRequest的字符串响应始终具有以下格式,希望这会有所帮助!

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">........</string>
回到你的代码,
data
当然是空的,因为
JSONObject json=JSONObject(data);
在调用请求之前调用了
onResponse
。我建议你参考我对以下问题的回答,了解你的问题:


希望这有帮助!

为什么不改用JsonObjectRequest呢?因为他会收到一个包含JSON@VadimCaen:啊,我明白了:)为什么不改用JsonObjectRequest呢?因为他收到一个包含JSON@VadimCaen:啊,我明白了:)很高兴能帮上忙:)很高兴能帮上忙:)
...
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();
}
...