Android - Volley不能将我的参数发送到服务器

3
我正在尝试使用Volley库向服务器发送数据,但出现了错误:

"在第0个字符处结束输入"

以下是我的代码:

public void postPrams(View view) {
        String tag_json_obj = "json_obj_req";

        String url = "http://Urlhere.com/register.php";

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        text.setText("done Post : "+response);
                        pDialog.hide();
                        Toast.makeText(getApplication(),"Done",Toast.LENGTH_LONG).show();

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("erorr", "Error: " + error.getMessage());
                Toast.makeText(getApplication(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
                pDialog.hide();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("first_name","Anwar");
                params.put("last_name","Samir");
                params.put("age", "1000");
                params.put("country", "egypt");
                params.put("city","le");
                params.put("street", "10sq");
                params.put("mobile_no", "0100000");
                params.put("login_name", "Asi");
                params.put("password", "123qwe");
                return params;
            }
        };

        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    }

请帮我解释为什么会发生这种情况。


记录响应并在此处显示。 - CodeWalker
4个回答

1
请查看 Volley 的 源代码
public JsonObjectRequest(int method, String url, JSONObject jsonRequest,Listener<JSONObject> listener, ErrorListener errorListener)

您在jsonRequest的位置上传递了null,这意味着您实际上没有在POST请求中传递任何数据。因此出现错误:"end of input at character 0 of。尝试将代码更改为:
public void postPrams(View view) {

    String tag_json_obj = "json_obj_req";

    String url = "http://Urlhere.com/register.php";

    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, getParams(),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    text.setText("done Post : "+response);
                    pDialog.hide();
                    Toast.makeText(getApplication(),"Done",Toast.LENGTH_LONG).show();

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("erorr", "Error: " + error.getMessage());
            Toast.makeText(getApplication(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
            pDialog.hide();
        }
    });

    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

private JSONObject getParams(){
    JSONObject params = new JSONObject();
    params.put("first_name","Anwar");
    params.put("last_name","Samir");
    params.put("age", "1000");
    params.put("country", "egypt");
    params.put("city","le");
    params.put("street", "10sq");
    params.put("mobile_no", "0100000");
    params.put("login_name", "Asi");
    params.put("password", "123qwe");
    return params;
}

当我使用这段代码时,会出现错误并且 Toast 消息告诉我:“org.json.JSONException: Value<!DOCTYPE 无法转换为 JSONObject 类型”。@W.K.S - Anwar Elsayed
非常感谢,这段代码解决了我的问题,你救了我的一天。 - Anwar Elsayed

0

看一下请求方法。是Post还是Get??你返回了一个值吗?还是空的?。

在你的服务器端尝试对参数进行回显。


0
 JSONObject params = new JSONObject();
 params.put("first_name","Anwar");
 params.put("last_name","Samir");
 params.put("age", "1000");
 params.put("country", "egypt");
 params.put("city","le");
 params.put("street", "10sq");
 params.put("mobile_no", "0100000");
 params.put("login_name", "Asi");
 params.put("password", "123qwe");

将 null 替换为参数。由于您没有发送任何正文,因此无法在服务器端接收。您放置参数的 Map 是用于标头而不是正文。您必须发送正文。

JsonObjectRequest request = newJsonObjectRequest(method,url,body,listener){
   ....headers here
};

看一下源代码。getParams()用于表单数据。https://android.googlesource.com/platform/frameworks/volley/+/1b4858087f25a8033c5136a96b750752b87e128c/src/com/android/volley/Request.java - W.K.S
getHeaders() 返回头信息的映射表。getParams() 返回表单数据的映射表。 - W.K.S

0

试一下这个

public void postDataVolley(Context context,String url,JSONObject sendObj){
try {
    RequestQueue queue = Volley.newRequestQueue(context);

    JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("Volley", "Volley JSON post" + response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Volley", "Volley JSON post" + "That didn't work!");
        }
    });

    queue.add(jsonObj);

}catch(Exception e){

}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接