1

I am working on web services in android,
i have three strings

String username = "Rajesh";
String password = "abc"
String usertype = "member";

and i want to send them to web service in the following form

 {"UserName":username,"Password":password,"UserType":usertype}  

like

 {"UserName":"Rajesh","Password":"abc","UserType":"member"}

web service url is something like : http://abc.xyz.org/api/login

i dont have any idea how to do it, please help me,
how can i convert strings to json like above and how can i pass it to web service
Thank you

2
  • 1
    it's a json object. Its creation is trivial Commented Jul 30, 2014 at 13:46
  • I dont have idea about it, can you please help me? Commented Jul 30, 2014 at 13:48

3 Answers 3

1

You can try this, JSONObject.put() uses a key value pair so do as needed:

try {
    JSONObject object = new JSONObject();
    object.put("username", "rajesh");
    object.put("password", "password");
} catch (JSONException e) {
    e.printStackTrace();
}

After forming the required JSONObject you can use HttpClient to send it onto a request, refer here.

You can also refer to my project on making Http requests on android here.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok Thank you for this and then how can i pass it to web service? can you please give me the full code for that ?
I've also shared a project with you. @RajeshSuthar
please vote up if you thing my question can be useful to others
1

Yes, You can use by Json Object Like below,

JSONObject jsonObj = new JSONObject();
jsonObj.put("UserName", "Rajesh"); 
jsonObj.put("Password", "abc");
jsonObj.put("UserType", "member");

If you send In webservice then

String json = jsonObj.toString();

StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);

also check pass json object in url

1 Comment

Ok Thank you for this and then how can i pass it to web service? can you please give me the full code for that ?
1
package com.aven.qqdemo;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonUutis {

private void toWebService(){
    JSONObject json = new JSONObject();
    String username = "Rajesh";
    String password = "abc";
    String usertype = "member";
    putJson(json,"UserName", username);
    putJson(json,"Password", password);
    putJson(json,"UserType", usertype);
    String jsonString = json.toString();
    //Send jsonString to web service.
}

private void putJson(JSONObject json, String key, String value){
    try {
        json.put(key, value);
    } catch (JSONException e) {
        //Error happened.
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.