如何从Volley中的JSONObject获取String响应

时间:2017-01-26 09:56:30

标签: android android-layout httprequest android-volley android-json

我使用volley创建了JSONRequest,它成功点击了服务,我检查了服务端,它接收了数据,然后发送" Success"作为回报。

问题在于,Service在输出中返回String,而Volley在输出中除了JSON Data。因此它执行onError方法,而不是onResponse

请指导我如何让它接受字符串响应,或者当您使用JSONObject作为请求时,它是不可能的。

    Request<JSONObject> jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, "http://192.168.0.101:8888/api/services/mytest",
            jsonParent, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("Success", response.toString());
            deleteFile();
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error", error.toString());
            deleteFile();
        }

    RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
    requestQueue.add(jsonObjectRequest);

2 个答案:

答案 0 :(得分:9)

您可以使用StringRequest代替JSONRequest。

StringRequest stringRequest = new StringRequest(methodType, url,
                    new Response.Listener<String>() {
                        @Override
                        public void onResponse(String response) {

                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    return headers == null ? super.getHeaders() : headers;
                }

                @Override
                public byte[] getBody() throws AuthFailureError {
                    return "Your JSON body".toString().getBytes();
                }
            };

getheaders方法是根据需要添加自定义标头,并getBody提供request body

答案 1 :(得分:0)

对于仍需要发送原始Json并接收String作为响应的用户:

默认情况下,正文由Scrollbar的{​​{1}}格式的请求参数组成。如Nayan正确建议的那样,在覆盖import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: MyWidget(), ), ); } } class MyWidget extends StatefulWidget { @override _MyWidgetState createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { final _scrollController = ScrollController(); @override Widget build(BuildContext context) { return Scaffold( body: Padding( padding: const EdgeInsets.all(15.0), child: Center( child: Column( children: <Widget>[ // ... Expanded( child: Scrollbar( controller: _scrollController, // <---- Here, the controller isAlwaysShown: true, // <---- Required child: SingleChildScrollView( controller: _scrollController, // <---- Same as the Scrollbar controller child: Text( "Long Text Here ...", textDirection: TextDirection.rtl, style: TextStyle(fontSize: 17.2), ), ), ), ), // ... ], ), ), ), ); } } 时,也应考虑覆盖application/x-www-form-urlencoded; charset=UTF-8以匹配新的正文格式。对于Json,应该是 StringRequest

getBody()