gpt4 book ai didi

java - 如何在php中接收POST

转载 作者:行者123 更新时间:2023-12-01 22:29:45 25 4
gpt4 key购买 nike

我正在尝试从我的 Android 客户端将数据发送到 php 端,下面是代码:

(我从互联网上的各种来源构建了此代码,因此我不确定这里是否一切正常)

private void postJSON(String myurl) throws IOException {
java.util.Date date= new java.util.Date();
Timestamp timestamp = (new Timestamp(date.getTime()));
try {
JSONObject parameters = new JSONObject();
parameters.put("timestamp",timestamp);
parameters.put("jsonArray", new JSONArray(Arrays.asList(makeJSON())));
parameters.put("type", "Android");
parameters.put("mail", "xyz@gmail.com");
URL url = new URL(myurl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setReadTimeout(10000 /* milliseconds */);
// conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestProperty( "Content-Type", "application/json" );
conn.setDoOutput(true);
conn.setRequestMethod("POST");
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(parameters.toString());
writer.close();
out.close();

int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode);

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();

while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println(response.toString());

}catch (Exception exception) {
System.out.println("Exception: "+exception);
}
}

我对这一行感到困惑:

writer.write(parameters.toString());

本质上我只发送一个字符串到 php 端。我如何在那里收到它? POST 变量名称是什么?

最佳答案

我对android完全没有经验,但是我猜测要么是某种数组,要么是json,你可以通过使用<?php echo var_dump($_POST); ?>创建一个页面来查看它发送的内容。并将 POST 请求发送到页面。

关于java - 如何在php中接收POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28059863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com