如何将Android应用程序的JSONObject传递给PHP文件?

我想发送一个简单的JSON对象到PHP服务器,但是当我尝试在服务器端检索该对象时,我的意思是我的$ _POST变量是空的。 服务器端是PHP 5.2,我使用的是Android模拟器10 ...有人可以看看我的代码并告诉我发生了什么问题吗? 非常感谢

public void uploadJSon() throws ClientProtocolException, IOException, JSONException{
       HttpClient httpclient = new DefaultHttpClient();
       String url = "http://so-dev-deb.niv2.com/suivi_activite/test.php";
       HttpPost httppost = new HttpPost(url);
       JSONObject json = new JSONObject();

       json.put("username", "bob");
       json.put("email", "test@testsite.com");
       List  nvps = new ArrayList ();

       nvps.add(new BasicNameValuePair("value", json.toString()));
       httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));                 

       URL test_url = new URL(url);
       URLConnection connection = test_url.openConnection();
       connection.setDoOutput(true);

       HttpResponse response;
       response = httpclient.execute(httppost);
       Log.i("NVPS",nvps.get(0).toString());
       Log.i("JSON",json.toString());
       Log.i("response", response.getEntity().getContent().toString());
       Log.i("response status",response.getStatusLine().toString());

       BufferedReader in = new BufferedReader(
             new InputStreamReader(
             connection.getInputStream()));

       String decodedString;

       while ((decodedString = in.readLine()) != null) {
          //System.out.println(decodedString);
          Log.i("info 10",decodedString);
       }
       in.close();

    }
  

服务器端test.php是:

<?php 
    $tmp = json_decode($_POST['value']); 
    var_dump($tmp); 
?>

我通常采用这种方法在Java代码中生成JSON对象:

StringWriter writer = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(writer);
jsonWriter.object();
jsonWriter.key("key1").value("test1");
jsonWriter.key("key2").value("test2");
jsonWriter.endObject();
String toSend = writer.toString();

我不是PHP的专家,但是,我工作的一个项目解码了我这样提交的json。

$jsonData = file_get_contents("php://input");

如果(isset($ jsonData)&&!empty($ jsonData)){$ this-> data = json_decode($ jsonData,true); }


public static String login(Context context, String email, String pwd) {

    HttpClient client = new DefaultHttpClient();


    HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
    // Limit
    HttpResponse response;
    JSONObject json = new JSONObject();
    try {

        Log.d("Debug","Login Url:" + context.getResources().getString(
                R.string.url) + context.getResources().getString(
                        R.string.login));

        HttpPost post = new HttpPost(context.getResources().getString(
                R.string.url) + context.getResources().getString(
                        R.string.login));

        post.setHeader("Accept", "application/json");
        post.setHeader("Content-type", "application/json");

        json.put("email", email);
        json.put("password", pwd);

        StringEntity se = new StringEntity("{"User":" + json.toString()
                + "}");
        post.setEntity(se);
        response = client.execute(post);

        if (response != null) {
            json = getResult(response);

            if(statusOK(json)){
                return new String(json.getString("token")); 
            }
        }
    } catch (Exception e) {
        Log.e("Error", "Error Login", e);
    }
    //  printJSON(json);

    return "ERROR";
}

注意我将JSON包装在用户中,因为服务器需要这个用于我的情况。

链接地址: http://www.djcxy.com/p/21939.html

上一篇: How do I pass a JSONObject from Android application to a PHP file?

下一篇: Passing array of parameters through get in rails