我想从我的下面Android客户端将数据发送到PHP端接收POST代码如下:如何在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", "[email protected]");
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变量名称?
您正在考虑具有名称的表单字段。在这种情况下,您正在发布您的JSON字符串。它没有名字。 [这个答案](http://stackoverflow.com/a/22662113/622391)告诉你如何接收JSON内容。 – 2015-01-21 05:07:14
我建议传递一些参数,并对这些参数进行一些处理,然后返回它们,在android端,获取这些参数,看看你是否得到这些处理参数。如果你得到那么你成功发布参数。 – 2015-01-21 05:22:37
@GauravDave,这正是我想要做的:) – User3 2015-01-21 05:29:56