如何在 PHP 服务器上显示 android POST 字符串?

How to display android POST string on PHP server?

我正在尝试将 json 字符串从我的 android 应用程序发送到我的 php 服务器。下面是我的 mobiledb_control.php 和 httpconnect.java

的完整代码

Log.v("HTTPSENDER","WORKED"); 运行s,我没有收到任何错误。 然而我的 error_log("in");不 运行.

如何将通过 android 发送的 JSON 显示到我的 error_log() 中?

HttpConnect.java:

public class HttpConnect {
    public HttpConnect(){

    }
    public void sendData(String jsonObject){
        try{
            URL url = new URL("http://www.alextanti.net/PHPDashboard/Backend/mobiledb_control.php");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);

            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
            output.write("json="+jsonObject);
            output.flush();
            output.close();
            Log.v("HTTPSENDER","WORKED");
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

mobiledb_control.php:

   <?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set("error_log", "../Logs/error.log");
error_reporting(E_ALL);
if(!empty($_POST['json'])){
  echo(var_dump($_POST['json']));
  error_log($_POST['json']);
}
$headers = apache_request_headers();

?>

在 PHP 代码中试试这个

并像这样打开错误报告

// Report all PHP errors (see changelog)
error_reporting(E_ALL);

print_r($_POST);

为了更清楚地说明你从 post 得到了什么。

  1. 使用php的编码和解码函数来制作和解析json。

     $request=json_decode($_POST['json']); // it gives the Array
      foreach($request as $values){
      echo($values['your_value1'])
       echo($values['your_value2'])
      }
    

请参考这个 url : http://php.net/manual/en/function.json-decode.php

在设计服务器-客户端通信时,必须确保客户端和服务器能够相互通信。考虑到这一点,您能否通过在 try 块内的代码中添加以下代码来提供服务器响应代码:

try {
 ...
 Log.d(TAG, "code: " + conn.getReturnCode());
 InputStream is = conn.getInputStream();
 String serverReply = readIt(is, 500);
 Log.d(TAG, serverReply);
 ...
}

public String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {
    Reader reader = null;
    reader = new InputStreamReader(stream, "UTF-8");
    char[] buffer = new char[len];
    reader.read(buffer);
    return new String(buffer);
}

It will return 200 and 401 respectively. Returns -1 if no code can be discerned from the response (i.e., the response is not valid HTTP).

干杯!

似乎已经修复了,但我不知道如何修复

PHP 文件:

<?php
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set("error_log", "../Logs/location.log");
error_reporting(E_ALL);
if(!empty($_POST['json'])){
  echo(var_dump($_POST['json']));
  error_log($_POST['json']);
}
?>

JAVA 文件:

public class HttpConnect {
    public HttpConnect(){

    }
    public void sendData(String jsonObject){
        try{
            URL url = new URL("http://www.alextanti.net/PHPDashboard/Backend/mobiledb_control.php");
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            BufferedWriter output = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
            output.write("json="+jsonObject);
            output.flush();
            output.close();
            Log.v("HTTPSENDER","WORKED");
            Log.v("HTTPSENDER",""+conn.getResponseCode());
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}