2

我是 Java 和 PHP 的初学者,我正在开发一个包含 2 部分的应用程序:

Android客户端(Java)和PHP服务器。

我尝试了许多可用的教程,并阅读了用户犯的错误,但没有成功!

这是我正在使用的教程之一:Java File

package org.postandget;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Main extends Activity {

TextView tv;
String text;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv  = (TextView)findViewById(R.id.textview);
    text    = "";

    try {
        postData();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void postData() throws JSONException{  
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://10.0.2.2/ReceiveLocation.php");
    JSONObject json = new JSONObject();

    try {
        // JSON data:
        json.put("name", "Fahmi Rahman");
        json.put("position", "sysdev");

        JSONArray postjson=new JSONArray();
        postjson.put(json);

        // Post the data:
        httppost.setHeader("json",json.toString());
        httppost.getParams().setParameter("jsonpost",postjson);

        // Execute HTTP Post Request
        System.out.print(json);
        HttpResponse response = httpclient.execute(httppost);

        // for JSON:
        if(response != null)
        {
            InputStream is = response.getEntity().getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            text = sb.toString();
        }

        tv.setText(text);

    }catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}
}

这是php文件

<?php
include('ConnectionFunctions.php');
Connection(); 

$json = $_POST['jsonpost'];
echo "JSON: \n";
echo "--------------\n";
var_dump($json);
echo "\n\n";

$data = json_decode($json);
echo "Array: \n";
echo "--------------\n";
var_dump($data);
echo "\n\n";

$name = $data->name;
$pos = $data->position;
echo "Result: \n";
echo "--------------\n";
echo "Name     : ".$name."\n Position : ".$pos; 
 ?>

这是我运行 php 时出现的错误

  Notice: Undefined index: HTTP_JSON in C:\xampp\htdocs\ReceiveLocation.php on line 5
  JSON: -------------- NULL Array: -------------- NULL
  Notice: Trying to get property of non-object in C:\xampp\htdocs\ReceiveLocation.php   on   line 17

  Notice: Trying to get property of non-object in C:\xampp\htdocs\ReceiveLocation.php on   line 18
  Result: -------------- Name : Position : 
4

3 回答 3

0

使用以下命令访问 JSON 数据:

$json = $_POST['jsonpost'];
于 2012-09-16T12:14:39.523 回答
0

您试图访问 php 文件中的无效字段,它应该是

      $json = $_POST['jsonpost'];

或者

       $json = $_REQUEST['jsonpost'];

如果您打算对数据进行数据库工作,请记住还要从 php 文件中的错误输入中消除数据。也可能你的本地主机路径应该从

     HttpPost httppost = new HttpPost("http://127.0.0.1/ReceiveLocation.php"); 

     HttpPost httppost = new HttpPost("http://10.0.2.2/ReceiveLocation.php");

希望我有所帮助。

于 2012-09-16T12:26:32.687 回答
0

您的代码有问题,JSON 数据仅添加到标头,而不是 HTTP 请求的 POST 部分。

所以当你输出:

print_r(getallheaders());

$headers = getallheaders();

$json = json_decode($headers['json']);

print_r($json);

你应该看到你的数据。我现在还没有修复,但我正在努力。

于 2013-02-11T13:47:55.047 回答