Java 发送带有参数和读取响应的POST请求

Java 发送带有参数和读取响应的POST请求,java,android,post,jsoup,httpurlconnection,Java,Android,Post,Jsoup,Httpurlconnection,我想向发送POST请求,参数为“text=”和“language=” 到目前为止,我已经在AsyncTask中尝试了三个过程:JsonObjectRequest、Jsoup.connect和HttpURLConnection,但似乎都不起作用 这是JsonObjectRequest的代码: Map<String, String> params = new HashMap(); params.put("text", "This is a text.");

我想向发送POST请求,参数为“text=”和“language=”

到目前为止,我已经在AsyncTask中尝试了三个过程:JsonObjectRequest、Jsoup.connect和HttpURLConnection,但似乎都不起作用

这是JsonObjectRequest的代码:

Map<String, String> params = new HashMap();
        params.put("text", "This is a text.");
        params.put("language", "en");

        JSONObject parameters = new JSONObject(params);

        JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, stringurl, parameters, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                //TODO: handle success
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                //TODO: handle failure
            }
        });

        Volley.newRequestQueue(this).add(jsonRequest);
这是Jsoup的代码:

Connection.Response response =
                Jsoup.connect(stringurl)
                        .userAgent("Mozilla/5.0")
                        .timeout(10 * 1000)
                        .method(Method.POST)
                        .data("text", text)
                        .data("language", "en")
                        .followRedirects(true)
                        .execute();
        Document document = response.parse();
它在“执行”时崩溃:

我应该接收一个JSON对象,例如:

{
    "taggedText": "John_NNP likes_VBZ the_DT blue_JJ house_NN at_IN the_DT end_NN of_IN the_DT street_NN ._. "
}
我在网上搜索了一些解决方案,但毫无结果。达到预期效果的最佳方法是什么?

尝试以下方法

 JSONObject parameters = new JSONObject();
 try {
      parameters.put("text" , "sample text");
      parameters.put("language", "en");
 } catch (JSONException e) {
      e.printStackTrace();
 }
 JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, stringurl, parameters,
     new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
           //TODO: handle success
        }
     }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                //TODO: handle failure
            }
      });

Volley.newRequestQueue(this).add(jsonRequest);
JSONObject参数=新的JSONObject();
试一试{
参数。输入(“文本”、“示例文本”);
参数。put(“语言”、“en”);
}捕获(JSONException e){
e、 printStackTrace();
}
JsonObjectRequest jsonRequest=新的JsonObjectRequest(Request.Method.POST、stringurl、参数、,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
//TODO:处理成功
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
错误。printStackTrace();
//TODO:处理失败
}
});
newRequestQueue(this.add)(jsonRequest);
更新

从您的
轨迹跟踪

的意外响应代码404

我使用
postman
测试你的URL,它会像你一样给出
响应404
。但是当我尝试不使用最后一个反斜杠时,效果很好

您应该URL中删除最后一个反斜杠。你的URL应该是

stringurl=“”


发布完整的
堆栈跟踪
。没错,就是这样,非常感谢!!遗憾的是,它返回一个空键,如下所示:{“taggedText”:“}不管给定的文本是什么。知道为什么吗?若你们不传递参数,你们会得到一个空字符串,就像你们已经得到的一样。如果您以正确的方式传递参数,那么您应该会得到类似
{“taggedText”:“\”\u$[hello\u FM\”\u$[“}
以下是我使用
postman
进行测试的屏幕截图。我像您一样传递参数,但仍然没有运气:(
{
    "taggedText": "John_NNP likes_VBZ the_DT blue_JJ house_NN at_IN the_DT end_NN of_IN the_DT street_NN ._. "
}
 JSONObject parameters = new JSONObject();
 try {
      parameters.put("text" , "sample text");
      parameters.put("language", "en");
 } catch (JSONException e) {
      e.printStackTrace();
 }
 JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, stringurl, parameters,
     new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
           //TODO: handle success
        }
     }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
                //TODO: handle failure
            }
      });

Volley.newRequestQueue(this).add(jsonRequest);