android.rws
android.rws

Reputation: 39

how to post data in JSON from android?

IN JSON Parsing in Android as i wants to post the data with below format on http URL,

 webdata={"email":"[email protected]","password":"123456"}  

how can i post this data by the use of httppost for this JSON on http url ?

Upvotes: 0

Views: 2168

Answers (3)

Varun Vishnoi
Varun Vishnoi

Reputation: 990

Use following method for generating json Object

private JSONObject getConvertedinJson(String email, String password) {

    JSONObject object = new JSONObject();
    try {
        object.put("email", email);
        object.put("password", password);
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return object;
}

after that use following method for posting json object on url

    public JSONObject getJSONFromUrl(String url, JSONObject jObj) {

    // Making HTTP request
    try {
        // Default Http Client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        // Http Post Header
        HttpPost httpPost = new HttpPost(url);
        StringEntity se = new StringEntity(jObj.toString());
        httpPost.setEntity(se);
        httpPost.setHeader("Accept", "application/json");
        httpPost.setHeader("Content-type", "application/json");
        // Execute Http Post Request
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    /*
     * To convert the InputStream to String we use the
     * BufferedReader.readLine() method. We iterate until the BufferedReader
     * return null which means there's no more data to read. Each line will
     * appended to a StringBuilder and returned as String.
     */
    try {
        // Getting Server Response
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        // Reading Server Response
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    Log.e("JSON Parser", jObj.toString());
    return jObj;

}

Now Use following statement to get json response.

JSONObject jsonObject = getJSONFromUrl(
                "www.url.com/api",
                getConvertedinJson("[email protected]",
                        "123456"));

Now you can easily Deserialize jsonObject according to need.

Note: Your Post Data must be Json Object. As I have seen webdata={"email":"[email protected]","password":"123456"} is not jsonObject here json Object is only {"email":"[email protected]","password":"123456"}

Upvotes: 1

Harish
Harish

Reputation: 3117

First you can prepare your json format data then send that as your request as per your requirement either GET or POST this need to be done with using Asynctask

JSONObject jObject = new JSONObject();
try{
jObject.put("email",urvalue);
jObject.put("password",urvalue);

List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("webdata", jObject.toString()));

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

}catch(Exception e){}

Upvotes: 1

Rajitha Siriwardena
Rajitha Siriwardena

Reputation: 2759

use StringEntity class.

StringEntity entity = new StringEntity("Your JSON String",HTTP.UTF_8);

and then add it to your HttpPost

httpPost.setEntity(entity);

Upvotes: 0

Related Questions