【问题标题】:Android POST JSON to server PHPAndroid POST JSON 到服务器 PHP
【发布时间】:2014-01-27 01:20:10
【问题描述】:

我在查找和使用我的应用发布到服务器的 JSON 对象时遇到了一些问题。
这是我的代码:

InputStream inputStream = null;
    String result = "";
    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();

        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = exmaplePrefs.getString("jsonString", "cant find json");
       // String json = "{\"twitter\":\"test\",\"country\":\"test\",\"name\":\"test\"}";

        // 5. set json to StringEntity
        StringEntity se = new StringEntity(json);

        // 6. set httpPost Entity
        httpPost.setEntity(se);

        // 7. Set some headers to inform server about the type of the content   
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");

        // 8. Execute POST request to the given URL
        HttpResponse httpResponse = httpclient.execute(httpPost);

        // 9. receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        String status = httpResponse.getStatusLine().toString();
        // 10. convert inputstream to string
        if (!status.equals("HTTP/1.1 500 Internal Server Error")){
            if(inputStream != null){
                result = convertInputStreamToString(inputStream);   
            }
            else{
                result = "Did not work!";
            }
        }else{
            System.out.println("500 Error");

        }



    } catch (Exception e) {
        Log.d("InputStream", e.getLocalizedMessage());
        System.out.println(e);
    }

    // 11. return result
    return result;
}

..

    endGame.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                    // call AsynTask to perform network operation on separate thread
                    //new HttpAsyncTask().execute("http://posttestserver.com/post.php");    
                    new HttpAsyncTask().execute("http://rugbyone.net84.net/recieve_data.php");
        }
    });

..

private static String convertInputStreamToString(InputStream inputStream) throws IOException{
    BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
    String line = "";
    String result = "";
    while((line = bufferedReader.readLine()) != null)
        result += line;

    inputStream.close();
    return result;

}

下面是PHP。如您所见,我尝试了各种不同的方法,但找不到 JSON。任何帮助,将不胜感激。

<?php
    /*  Database connection  */

     if( isset($_POST["json"]) ) {  

     $data->msg = strrev($data->msg);
     $data = json_decode($_POST["json"]);
     echo json_encode($data);
     echo $GLOBALS['HTTP_RAW_POST_DATA'];
}
     $json = file_get_contents("php://input");
     $json2 = $_SERVER['HTTP_JSON'];
    // Lets parse through the JSON Array and get our individual values
    $parsedJSON = json_decode($json, true);
    var_dump($json);
    var_dump($parsedJSON);
    var_dump($json2);
    var_dump($data);


 ?> 

【问题讨论】:

  • 你有什么问题?请解释一下,如果您在 log cat 中有错误,请将其附在您的问题中
  • $data-&gt;msg = strrev ... 的意义何在?无论如何,您在下一行替换$data。如果你没有从 php://input 得到任何东西,那么你的客户端代码首先不会向 PHP 发送任何东西。
  • 它们都是我发现的“抓取” json 对象的所有方法,但都不起作用
  • @MarcB 我测试了客户端但发布到这个服务器 - posttestserver.com/data/2014/01/26/15.33.18171433763 并且你可以看到 json 对象在那里......
  • @JackDuong 在 logcat 中没有错误。我得到OK 200!所以应用程序正常工作

标签: php android json


【解决方案1】:

看起来您只是将原始帖子正文发送到您的 PHP 站点。您的 PHP 需要一个名为 json 的 post 变量,但您提供的 Android 代码中并未定义该变量。

您应该像这篇文章中那样使用 NameValuePair:

Apache HTTP Client, POST request. How to correctly set request parameters?

【讨论】:

    猜你喜欢
    • 2016-07-26
    • 2023-03-05
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多