简体   繁体   中英

Method to convert JSONObject to List<NameValuePair>

Hi iam creating an android application. In my application i have some form fields like edittext and radio buttons i am creating a JSONObject by retrieving text from all the form fields. JsonObject is created successfully. Now i want to pass this object to my PHP page where i have written code for getting this details and storing it in database. My problem is i am not understanding how to send this JSON object through httpPost or httpGet method. Only way i know is send parameters through List<NameValuePair> so i'm trying to convert JSONObject to List<NameValuePair> . Can anybody provide a method which can directly convert my JSONObject to List<NameValuePair> . Is there any predefined method for doing this. Or can any one provide solution where i can directly send by JSONObject to PHP and retrieve there.

Pass your JSONObject as a string to the String Entity constructor and then pass it to setEntity()

Sample:

    HttpPost request = new HttpPost("//website");
    StringEntity params =new StringEntity("passmyjson=" + yourJSONOBject.toString());
    request.addHeader("content-type", "//header");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);

in php File to check that it works;

<?php
print_r($_POST);
$json_string = $_POST['passmyjson']; 
$json = json_decode($json_string);
print_r($json);
?>

You can do that with Apache HttpClient . I assume you have already a PHP handler that handles this request. Simply,

  • Create your JSONObject
  • Put your desired values
  • Send that json to php handler
  • You need to send request as application/x-www-form-urlencoded

Let's call url : http://your_php_service.com/handleJson ;

HttpClient httpClient = new DefaultHttpClient();
JSONObject json = new JSONObject();
json.put("key", "val");

try {
    HttpPost request = new HttpPost("http://your_php_service.com/handleJson");
    StringEntity params = new StringEntity("json=" + json.toString());
    request.addHeader("content-type", "application/x-www-form-urlencoded");
    request.setEntity(params);
    HttpResponse response = httpClient.execute(request);
} catch (Exception ex) {

} finally {
    httpClient.getConnectionManager().shutdown();
}

The format of request param will be ;

json={"key": "val"}

And you can handle this on php side like;

<?php
.....
$json = $_POST["json"]; // This will be json string
.....

Thank you all i got it

I added the following lines to my android Activity class

 DefaultHttpClient httpClient = new DefaultHttpClient();
 HttpResponse httpResponse;
 HttpPost httppost = new HttpPost(link);                //-->link is the php page url 
 httppost.setEntity(new StringEntity(obj.toString()));  //-->obj is JSONObject
 httpResponse = httpClient.execute(httppost);
 HttpEntity httpEntity = httpResponse.getEntity();

and in my php file i have added the following code

$msg=json_decode(file_get_contents('php://input'), true); 

To get particular value from recieved Json string i added this $data = $msg['name'] ;

It is working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM