Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.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中的POST方法';行不通_Java_Android - Fatal编程技术网

切换到java中的POST方法';行不通

切换到java中的POST方法';行不通,java,android,Java,Android,我不明白如何在我的HttpsURLConnection中切换到POST方法。在调试器上,请求方法也在setRequestMethod之后。你能告诉我我的错在哪里吗 try { URL url=new URL("https://smartmates.herokuapp.com"); HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();

我不明白如何在我的HttpsURLConnection中切换到POST方法。在调试器上,请求方法也在setRequestMethod之后。你能告诉我我的错在哪里吗

try {
                URL url=new URL("https://smartmates.herokuapp.com");
                HttpsURLConnection connection= (HttpsURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoInput(true);
                connection.setDoOutput(true);
                //I'll add some params here
                connection.disconnect();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

非常感谢。

这是一段代码,我用来发布
字符串mensaje
并接收
rta.ToString()
。我认为
DoSetInput(true)
是一个错误,因为您希望发送一个POST(输出),并最终得到响应

 `
    String urlParametros = "<?xml version=\"1.0\"?>";
    urlParametros = urlParametros + mensaje;
    byte[] postDatos = urlParametros.getBytes(StandardCharsets.UTF_8);
    try {
        URL miurl = new URL(url);
        con = (HttpURLConnection) miurl.openConnection();
        con.setDoOutput(true);
        con.setRequestMethod("POST");
        //******…………..
        con.setRequestProperty("User-Agent", "Java client");
        con.setRequestProperty("Content-Type", "text/xml");
        try (DataOutputStream datos = new DataOutputStream(con.getOutputStream())) {
            datos.write(postDatos);
        }
        StringBuilder rta;
        try (BufferedReader entrada = new BufferedReader(
                new InputStreamReader(con.getInputStream()))) {
            String linea;
            rta = new StringBuilder();
            while ((linea = entrada.readLine()) != null) {
                rta.append(linea);
                rta.append(System.lineSeparator());
            }
        }
        return rta.toString();
    } finally {
        con.disconnect();
    }´
`
字符串UrlParameteros=“”;
UrlParameteros=UrlParameteros+mensaje;
byte[]postDatos=urlParameteros.getBytes(StandardCharsets.UTF_8);
试一试{
URL miurl=新URL(URL);
con=(HttpURLConnection)miurl.openConnection();
con.设置输出(真);
con.setRequestMethod(“POST”);
//******…………..
con.setRequestProperty(“用户代理”、“Java客户端”);
con.setRequestProperty(“内容类型”、“文本/xml”);
try(DataOutputStream datos=newdataoutputstream(con.getOutputStream())){
datos.write(postDatos);
}
StringBuilder rta;
try(BufferedReader entrada=新的BufferedReader(
新的InputStreamReader(con.getInputStream())){
弦线;
rta=新的StringBuilder();
而((linea=entrada.readLine())!=null){
rta.附加(线性);
append(System.lineSeparator());
}
}
返回rta.toString();
}最后{
con.disconnect();
}´
希望能有帮助


Daniel

检查此项非常感谢,您是否也可以告诉我请求属性(如用户代理和内容类型)是否是必需的?我猜这段代码将发送一个xml正文,对吗?如何发送JsonObject?不确定。我认为服务器有“指令”。在我的例子中,我在POST中发送XML。尝试connect.addRequestProperty(“接受”,“应用程序/json”);我尝试了你的解决方案,但没有成功,所以我尝试在eclipse中运行相同的代码,但我不明白为什么它会成功。可能是android studio中存在错误吗?因为在eclipse中,一切都正常工作……感谢你的帮助我不明白:我给你的代码片段在eclipse中工作,但在android中不工作Studio?它只需要明显的导入,使用来自UI的单独线程,并在清单中使用Internet权限。在eclipse中,发布数据部分起作用,但问题是,在android Studio中运行该代码时,requestMethod在整个运行时都保持GET