【问题标题】:calling json data to php at server side in android在android的服务器端调用json数据到php
【发布时间】:2014-03-04 17:40:58
【问题描述】:

嗨,

我使用这个类向服务器发出请求,该请求由 json 数据对象组成。 类是:-

public class HttpClient {

private static String URL = "localhost/json/json_handle.php";

public String postJsonData(String data) {

    try {
        StringBuffer buffer = new StringBuffer();
        // Apache HTTP Reqeust
        System.out.println("Sending data..");
        System.out.println("Data: [" + data + "]");
        org.apache.http.client.HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(URL);
        List<NameValuePair> nvList = new ArrayList<NameValuePair>();
        BasicNameValuePair bnvp = new BasicNameValuePair("json", data.toString());
        // We can add more

        nvList.add(bnvp);
        post.setEntity(new UrlEncodedFormEntity(nvList));

        HttpResponse resp = client.execute(post);
        // We read the response
        InputStream is = resp.getEntity().getContent();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(is));
        StringBuilder str = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            str.append(line + "\n");
        }
        is.close();
        buffer.append(str.toString());
        // Done!

        return buffer.toString();
    } catch (Throwable t) {
        t.printStackTrace();
    }

    return null;
}
}

然后我在服务器端使用 php 类从请求中获取 json 对象。但是,在服务器端我什么也没得到。即使我使用$_REQUEST 方法,此方法之后的代码也不起作用。

这是我的 php 文件:-

    <?php
    $file = fopen("MyFile.txt" ,"w");
    $int = $_REQUEST;
    fwrite($file,"aaa");

    //$input =$_REQUEST['json'];
    fwrite($file,"HELLO  111");

    //$data = json_decode($input,true);

    /*print_r($input);
    // get values
    $firstname = $input->firstName;
    $surename = $input->lastName;
    $age = intval($input->age);

    // check values
    if (isset($firstname) && !empty($firstname) && 
        isset($surename) && !empty($surename) &&
        isset($age) && is_numeric($age))
    {
        // do something
        echo "Hello ".htmlspecialchars($firstname)." ".htmlspecialchars($surename)."!<br>";
        echo "You are $age years old! Wow.";
    }
    else
    {
        echo "Some values are missing or incorrect";
    }*/

    //fwrite($file, $data);

    fclose($file);

?>

关于这个问题有什么建议吗???

【问题讨论】:

  • 不起作用是什么意思?
  • 您是否尝试使用 $_GET 参数手动调用您的 php 并检查日志记录是否有效?
  • 我在 php 文件中使用文件处理。在 $int = $_REQUEST; 之后我的代码行不通,因为它没有创建任何文件。这意味着它给出了一个错误。
  • 你为什么要做 $int = $_REQUEST?尝试使用 ini_set("display_errors", 1) 运行您的代码。这应该会给您导致此行为的错误。或者您在客户端代码中收到响应代码 500?
  • 请查看我的回答她Android JSON example

标签: php android json


【解决方案1】:

感谢朋友的帮助。 我得到了解决方案,现在该程序在 localhost 和在线上都能完美运行。

对于本地主机,我们只需将 URL 指定为:-

1.1.1.1/json/json_handle.php

其中 1.1.1.1 是您的 IP 地址。

再次感谢各位朋友。

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 2011-02-02
    • 2017-02-11
    • 2012-07-19
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多