Skip to content Skip to sidebar Skip to footer

Error Posting Json Data From Android

I am trying to post something to my mysql database using PHP script. I have used JSON parsing. The app shows post successfully added when i click the button but, there is an error

Solution 1:

You should pass the JSONObject not the String. This is how you have to pass the JSONObjects..

JSONObject jsonObjSend = newJSONObject();

        try {
            jsonObjSend.put("username",userName.getText().toString().trim());
            jsonObjSend.put("password",userPhoneNumber.getText().toString().trim());

        } 
        catch (JSONException e) {
            e.printStackTrace();
        }

HTTPPOST

DefaultHttpClienthttpClient=newDefaultHttpClient();
        HttpPosthttpPostRequest=newHttpPost(URL);

        StringEntity se;

        se = newStringEntity(jsonObjSend.toString());

        // Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Content-type", "application/json");
        httpPostRequest.setHeader("Accept-Charset", "utf-8");
        HttpResponseresponse= (HttpResponse) httpclient.execute(httpPostRequest);

Post a Comment for "Error Posting Json Data From Android"