Php JSON发送到服务器

Php JSON发送到服务器,php,android,json,Php,Android,Json,我必须从我的android应用程序向服务器发送一些JSON数据 这是我写的代码 public static String POST(String url, String id, int status, String cardno,String orderno){ InputStream inputStream = null; String result = ""; try { // 1. create HttpClient HttpCl

我必须从我的android应用程序向服务器发送一些JSON数据

这是我写的代码

public static String POST(String url, String id, int status, String cardno,String orderno){
    InputStream inputStream = null;
    String result = "";

    try {

        // 1. create HttpClient
        HttpClient httpclient = new DefaultHttpClient();
        HttpConnectionParams.setConnectionTimeout(httpclient.getParams(), 10000);
        // 2. make POST request to the given URL
        HttpPost httpPost = new HttpPost(url);

        String json = "";

        // 3. build jsonObject
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("order_number", orderno);
        jsonObject.put("id", id);
        jsonObject.put("status", status);
        jsonObject.put("card_no", cardno);

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


        // 4. convert JSONObject to JSON to String
       // json = jsonObject.toString();
       // httpPost.setParams("json", json.toString());
        System.out.println("json is"+jsonObject.toString());
        httpPost.getParams().setParameter("jsonpost", postjson);

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

        // 6. set httpPost Entity
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));
        httpPost.setEntity(se);



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

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

        // 10. convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

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

    // 11. return result
    System.out.println("result is"+result);
    return result;
}


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脚本如下所示:

<?php

error_reporting(E_ALL & ~E_NOTICE);
    file_put_contents("postData.txt",$_REQUEST);

    if(isset($_REQUEST['jsonpost']))
    {
        echo 'passed';
    }else{
        echo "NOT RUNNING";
    }
    exit;
?>

但它仍然不起作用。要在我的php脚本中进行任何更改吗?

$\u REQUEST
是一个类似于
$\u POST&$\u GET
的数组,因此假设您只想知道其中的内容,而不是经过任何优雅的显示
print\u r()

file_put_contents("postData.txt",print_r($_REQUEST, true));

尝试将post数据设置为如下请求:

//passes the results to a string builder/entity
StringEntity se = new StringEntity(jsonObject.toString());

//sets the post request as the resulting string
httpPost.setEntity(se);

//sets a request header so the page receving the request
//will know what to do with it
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

//Handles what is returned from the page 
ResponseHandler responseHandler = new BasicResponseHandler();
HttpResponse httpResponse = httpclient.execute(httpPost, responseHandler);
请注意,这不是完整的代码,只是补充代码的一部分,因此请在示例中使用它(所有变量的名称都与您命名它们的名称相同,因此应该很容易:)


还要确保您的JSONObject具有
jsonpost
键,因为当您这样发布它时,来自JSONObject的
key->values
将成为
key->values
变量中的
post

很抱歉,错过了php脚本。我尝试了按照您的解决方案进行操作。对不起,它没用。
//passes the results to a string builder/entity
StringEntity se = new StringEntity(jsonObject.toString());

//sets the post request as the resulting string
httpPost.setEntity(se);

//sets a request header so the page receving the request
//will know what to do with it
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");

//Handles what is returned from the page 
ResponseHandler responseHandler = new BasicResponseHandler();
HttpResponse httpResponse = httpclient.execute(httpPost, responseHandler);