dimanche 28 juin 2015

Send Data to Server in Android using JSON

I want to send data from android app to remote server. I have tried to send data as a sample to the server but it is working. I am uploading my code below. I am not sure but may be my data sending formate is wrong.

The link need to show a success message. Unfortunately it not showing any change.

public void postData() throws JSONException {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://ift.tt/1dfhGlr");
    JSONObject json = new JSONObject();

    try {
        // JSON data:
        json.put("task_title", "Fahmi Rahman");
        json.put("task_details", "sysdev");

        JSONArray postjson=new JSONArray();
        postjson.put(json);

        // Post the data:
        httppost.setHeader("json",json.toString());
        httppost.getParams().setParameter("jsonpost",postjson);

        // Execute HTTP Post Request
        System.out.print(json);
        HttpResponse response = httpclient.execute(httppost);

        // for JSON:
        if(response != null)
        {
            InputStream is = response.getEntity().getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            StringBuilder sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //text = sb.toString();
        }

        //tv.setText(text);

    }catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

And I need to send data this formate

{"task":{
            "task_title":"All json formate",
            "task_details":"View All task list, View task list by user id, Add new task"}}

Can anyone tell me how it may work?

Aucun commentaire:

Enregistrer un commentaire