2015-01-21 62 views
0

我想从我的下面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变量名称?

+2

您正在考虑具有名称的表单字段。在这种情况下,您正在发布您的JSON字符串。它没有名字。 [这个答案](http://stackoverflow.com/a/22662113/622391)告诉你如何接收JSON内容。 – 2015-01-21 05:07:14

+0

我建议传递一些参数,并对这些参数进行一些处理,然后返回它们,在android端,获取这些参数,看看你是否得到这些处理参数。如果你得到那么你成功发布参数。 – 2015-01-21 05:22:37

+0

@GauravDave,这正是我想要做的:) – User3 2015-01-21 05:29:56

回答

1

我对android绝对没有经验,但是我猜不管是某种数组还是作为json,你可以通过在页面上使用<?php echo var_dump($_POST); ?>并发送POST请求到页面来查看它发送了什么。

+0

谢谢,我会试试这个! – User3 2015-01-21 05:17:36

+0

如果我使用上述方法,我得到'array(0){}'作为回显。 – User3 2015-01-21 05:21:09

+0

嗯,你确定它正在发送POST数据吗?试试'<?php echo $ HTTP_RAW_POST_DATA; '>正如答案中所建议的SimonMᶜKenzie所链接的 – CarlosAllende 2015-01-21 05:23:07

1

它看起来像你正在发送二进制数据,而不是name-value后参数。

试试这个在你的php代码:echo file_get_contents("php://input");,看看它打印什么。

另外,您可以使用HTTPClientRestHTTPClient from loopj作为您的android客户端。

+0

我一直在使用默认的HTTP客户端,它工作正常。我只是想用HTTPUrl Connection来尝试它。 Thansk的意见:) – User3 2015-01-21 06:32:45

1

像CarlosAllende我没有Android的经验。但希望这可以帮助你。

这就是你在'$ _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/auth‌​enticate_user.php</b> on line <b>15</b><br /> 

如果是这样,它是一个可捕获的致命错误,告诉'类StdClass的对象不能转换为字符串'。所以如果我是正确的,它试图把你的$ _POST数组中的对象,它是无法将其转换为字符串。而是在$ _POST数组中出现错误。

希望这可以帮助

+1

感谢ton @Vishal。我开始将整个方法转换为名称值对,以提高可读性和干净的代码:) – User3 2015-01-21 06:44:54