如何接收POST在PHP [英] How to receive POST in php
问题描述
我试图将数据从我下方的Android客户端发送到PHP一边是code:
I am trying to send data to php side from my Android client below is the code:
(我已经建造从各种渠道这个code在互联网上,所以我不知道,如果一切顺利这里)
(I have constructed this code from various sources on the internet so I am not sure if all is well here)
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);
}
}
我很困惑在这行:
I am confused at this line:
writer.write(parameters.toString());
基本上我只是将一个字符串的PHP端。我如何接受它呢?会是什么POST变量名?
essentially I am only sending one string to the php side. How do I receive it there? What would be the POST variable name?
推荐答案
像CarlosAllende我对Android的没有经验。但希望这可以帮助你。
like CarlosAllende i have no experience in Android. But hope this might help you.
这是你在你的'$ _POST'得到什么?
this is what you get in your '$_POST' ?
Response Code : 200 01-21 10:46:44.969: I/System.out(21561): <br />
<b>Catchable fatal error</b>: Object of class stdClass could not be converted to string in
<b>/opt/lampp/htdocs/Pdrive/digital_anniversaries/wordpress-3.9.1/wordpress/authenticate_user.php</b> on line <b>15</b><br />
如果因此它是一个捕获的致命错误,告诉类stdClass的客体不能转换成字符串。所以,如果我是正确的它试图把对象插入到$ _POST数组,它是无法将其转换为字符串。而是你获得你的$ _POST数组错误。
if so it is a Catchable fatal error telling that 'Object of class stdClass could not be converted to string'. so if i am correct it's trying to take object into your $_POST array and it's is unable to convert it to string. and instead your getting the error in your $_POST array.
希望这有助于
这篇关于如何接收POST在PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!