在给定的教程中,如何在android中使用截击发送Json POST请求?

在给定的教程中,如何在android中使用截击发送Json POST请求?,android,android-volley,Android,Android Volley,嗨,我是Android的初学者,我正在学习Api调用。我得到了一个截击教程,它使用GET接收响应。现在我想用截击发送Post请求。我不知道怎么做,在给定的教程中,POST的代码是什么。请指导我发送Post请求。我正在学习的教程的链接是您必须使用以下代码发送请求 // Instantiate the RequestQueue. RequestQueue queue = Volley.newRequestQueue(this); String url ="http://www.google.com"

嗨,我是Android的初学者,我正在学习Api调用。我得到了一个截击教程,它使用GET接收响应。现在我想用截击发送Post请求。我不知道怎么做,在给定的教程中,POST的代码是什么。请指导我发送Post请求。我正在学习的教程的链接是

您必须使用以下代码发送请求

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Display the first 500 characters of the response string.
        mTextView.setText("Response is: "+ response.substring(0,500));
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        mTextView.setText("That didn't work!");
    }
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
//实例化RequestQueue。
RequestQueue=Volley.newRequestQueue(this);
字符串url=”http://www.google.com";
//从提供的URL请求字符串响应。
StringRequest StringRequest=新的StringRequest(Request.Method.POST,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//显示响应字符串的前500个字符。
mTextView.setText(“响应为:”+Response.substring(0500));
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
setText(“那没用!”);
}
});
//将请求添加到RequestQueue。
添加(stringRequest);
试试看{
/**json对象参数**/
JSONObject JSONObject=新的JSONObject();
jsonObject.put(“你好”,“你好”);
e(“jsonObject参数”,jsonObject.toString()+”);
/**网址*/
字符串url=”http://google.com"
progress.setVisibility(View.VISIBLE);
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,url,jsonObject,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject JSONObject){
progress.setVisibility(View.GONE);
Log.e(标记“Response”+jsonObject.toString());
}
},new Response.ErrorListener(){
@凌驾
公共错误响应(截击错误截击错误){
progress.setVisibility(View.GONE);
Log.e(标记、截击错误);
Util.showtoos(活动“请重试”);
}
});
add(jsonObjectRequest);
}捕获(JSONException e){
progress.setVisibility(View.GONE);
Log.e(TAG,e);
}
捕获(例外e){
progress.setVisibility(View.GONE);
Log.e(TAG,e);
}
}
}
RequestQueue=Volley.newRequestQueue(this);
add(jsonObjectRequest);
您可以按照以下步骤操作:

StringRequest-request=新的StringRequest(Method.POST,
“发布url”,
新回应者(){
@凌驾
公共void onResponse(字符串响应){
Log.d(“响应”,响应);
}捕获(例外e){
e、 printStackTrace();
}
}
},新的ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“响应”、“错误”);
}
}) {
//后参数
@凌驾
受保护的映射getParams()引发AuthFailureError{
Map params=新的HashMap();
参数出售(“参数”、“参数”);
返回参数;
}
};
//2.使用自定义截击管理器发送请求或类似的方式

newRequestQueue(mCtx).add(请求)

这家伙似乎在他的帖子中涵盖了这一切,我编辑了我的代码。没用的!请给我错误什么样的错误来了,还请给我提供你有postedI没有发布任何数据的post数据,请你告诉我如何发布数据?
    try {
        /** json object parameter**/
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("hello", "hello");
        Log.e("jsonObject params", jsonObject.toString() + "");
        /**URL */
        String url ="http://google.com"

        progress.setVisibility(View.VISIBLE);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                progress.setVisibility(View.GONE);
                Log.e(TAG, "Response " + jsonObject.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                progress.setVisibility(View.GONE);
                Log.e(TAG, volleyError);
                Util.showToast(activity, "Please try again");
            }
        });
        requestQueue.add(jsonObjectRequest);

    } catch (JSONException e) {
        progress.setVisibility(View.GONE);
        Log.e(TAG, e);
    }
    catch (Exception e) {
            progress.setVisibility(View.GONE);
            Log.e(TAG, e);
        }
    }
}

RequestQueue queue = Volley.newRequestQueue(this);
queue.add(jsonObjectRequest);
StringRequest request = new StringRequest(Method.POST,
        "post url",

        new ResponseListener() {

            @Override
            public void onResponse(String response) {
                Log.d("response", response);

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

        }, new ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                     Log.e("response","error");
            }

        }) {

   // post params
    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String, String> params = new HashMap<String, String>();
        params.put("param", "param");
        return params;
    }

};
//2.Use your custom volley manager  send request or like this