简体   繁体   中英

How to send JSON object over request to web service in android

I would like to pass JSON object as request to web service like i mentioned below.

Result:{
 "email":"xxxxxxx",
 "password":"xxxxxx",
 "Marks":[
  {
   "mark1":"50",
   "mark2":"70"
  }
],
"firstname":"xxxx",
"lastname":"xxxxx"
}

My code: ...

HttpPost httppost = new HttpPost("My Url");
    httppost.setEntity(new StringEntity(**message**.toString(), "UTF-8"));

Here message should have json object in above format.How could i format JSON object?

Thanks.

If you arer having trouble in implementing above json object at android side then you can construct it like below,

JSONObject message = new JSONObject();

JSONObject mParams = new JSONObject();
mParams.put("email", "xxxx");
mParams.put("password", "xxx");

JSONArray markArray = new JSONArray(); 
JSONObject markObj = new JSONObject();
markObj.put("mark1", "50");
markObj.put("mark2", "70");
markArray.put(markObj);

mParams.put("Marks", markArray);

mParams.put("FirstName", "xxxx");
mParams.put("lastname", "xxxx");

message.put("Result",mParams);

Now in your code

    HttpPost httppost = new HttpPost("My Url");
    httppost.setEntity(new StringEntity(**message**.toString(), "UTF-8"));

You can just send it like a String:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("json", message.toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

and

$data = json_decode($json);

First of all, the JSON which you have mentioned above is INVALID .

Now,

Here message should have json object in above format.How could i format JSON object?

=> There are 2 ways to do that:

1) Create a request structure by using JSONObject or JSONArray classes, something like:

JSONObject objRequest = new JSONObject(); objRequest.putString("email","xxxx"); objRequest.putString("password","xxxx");

while setting entity inside HttpPost object, convert it into the String value.

2) Bad way, simple generate a string value with escape sequences, something like:

String strRequest = "{\"email\":\"xxxxxxx\",\"password\":\"xxxxxx\"}";

This may help you..Use GSON library. GSON is a Google library to parse JSON resources. With regards to the Marshalling, it basically means a way to translate data structures (like objects in an OOP language) to JSON... for instance :

// Java object
class Book {
  private String title;
  private String isbn;
  private Set<author> authors;
}

@Entity
class Author {
  private String firstName;
  private String lastName;
}

To...

{title:   "Vocation Createurs",
 isbn:    "2829302680",
 authors: [{firstName: "Barbara", lastName: "Polla"},
           {firstName: "Pascal",  lastName: "Perez"}]} 

您还可以使用现有的库,例如android-json-rpc

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