java - 将 java 示例从 GET 转换为 POST

标签 java android http post get

我没有 Java 经验,我得到了一些 XML 交互的工作示例代码,我唯一的问题是我需要 POST 数据(用户名/密码)

相关代码为:

static String myUrlString = "http://www.grupoandroid.com/interface/mobile/index.php";
protected TheParser(){
    try {
        this.myUrl = new URL(myUrlString);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }
}



protected InputStream getInputStream() {
    try {
        return myUrl.openConnection().getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

最佳答案

您可以使用HttpURLConnection

protected InputStream getInputStream() {
    try {
        HttpURLConnection con = new HttpURLConnection(myUrl);
        con.setRequestMethod("POST");
        //writeout data to the output stream
        return con.getInputStream();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

关于java - 将 java 示例从 GET 转换为 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5931443/

相关文章:

Java-Vaadin : NativeSelect setValue not working

java - 为什么 Tomcat 7 服务器在 Ubuntu 上停止?

android - 如何从 Android 中的 Google Play 游戏库在 Google Plus 上发布排行榜

javascript - 我如何迭代数组并将项目传递给方法并且所有方法都应该同步运行?

api - Promise 拒绝 : No provider for UserDetails!(UserDetails 是 Model 类)

http - HTTP header Vary :*是什么意思

java - javaFX 8 中的投票栏

java - Jcreator运行文件时出错

android - Flex Hero Mobile,弹出 View 时共享数据

java - 将反射与 Realm 结合使用 (Android)