开发者

How do I pass a JSONObject from Android application to a PHP file?

I want to send a simple JSON object to PHP server, but when I try to retrieve that object on the server side, there is nothing I mean my $_POST variable is empty. The server side is PHP 5.2 and I'm using android emulator 10... Could someone have a look on my code and tell me what is going wrong? Thanks a lot

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 <NameValuePair> nvps = new ArrayList <NameValuePair>();

       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();

    }

The server side test.php is :

<?php 
    $tm开发者_如何学运维p = json_decode($_POST['value']); 
    var_dump($tmp); 
?>


I usually take this approach for generating a JSON object in Java code:

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();


I'm no expert in PHP, However, a project I worked on decoded the json that I submitted like this.

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

if(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";
}

Note I wrap the JSON in the User as the server required this for my case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜