Java中带有String参数的POST请求

时间:2020-10-20 12:17:55

标签: java android post bufferedwriter bufferedoutputstream

我正在尝试将发布请求从我的Android应用发送到Express服务器。我创建一个JSON,然后将其转换为字符串,当我记录它时,它看起来像:

{"customerID":"testID"
"productID":"testID",
"productCategory":"testCategory", "city":"Oslo",
"country":"Norway",
"deviceType":"Android",
"token":"5f883e921523d77504eaea12"}

然后,我尝试通过此函数在请求正文中发送它:

try {
            URL url = new URL(urlString);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            out = new BufferedOutputStream(urlConnection.getOutputStream());
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));

            writer.write(data);
            writer.flush();
            writer.close();
            out.close();
            urlConnection.connect();


} catch (Exception e) {
            Log.d("err", e.getMessage());
}

当我将参数记录到后端时,它会转换为类似的内容:

{
'{"customerID":"testID""productID":"testID","productCategory":"testCategory","city":"Oslo","country":"Norway","deviceType":"Android","token":"5f883e921523d77504eaea12"}': ''
}

我认为问题出在缓冲写入器中,但我不知道如何解决。

0 个答案:

没有答案
相关问题