Home » Android » JSON_ERROR_SYNTAX sending Json request via Android App

JSON_ERROR_SYNTAX sending Json request via Android App

Posted by: admin November 1, 2017 Leave a comment

Questions:

I m trying to send a Json Post request using my Android Application.
But something weird happens.

Here is my Android Code:

try {
        URL url = new URL(BASE_URL + params[0]);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type","application/json");
        connection.connect();

        //JSonObject

        JSONObject json = new JSONObject();
        for(int i = 0 ; i < jsonValues.size(); i +=2){
            json.put(jsonValues.get(i), jsonValues.get(i + 1));
        }
        jsonValues.clear();


        DataOutputStream output = new DataOutputStream(connection.getOutputStream());
        //String encoded= URLEncoder.encode(json.toString(),"UTF-8");
        output.writeBytes(URLEncoder.encode(json.toString(),"UTF-8"));
        output.flush();
        output.close();

        int HttpResult = connection.getResponseCode();
        if(HttpResult ==HttpURLConnection.HTTP_OK){
            BufferedReader br = new BufferedReader(new InputStreamReader(
                    connection.getInputStream(),"UTF-8"));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                String lineDecoded = URLDecoder.decode(line, "UTF-8");
                sb.append(lineDecoded + "\n");
            }
            br.close();
            System.out.println(""+sb.toString());
            if(sb != null){
                return sb.toString();
            }else{
                return null;
            }


        }else{
            Log.d("ERRORRRRR",connection.getResponseMessage());
            return connection.getResponseMessage();
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
        return e.toString();
    } catch (IOException e) {
        e.printStackTrace();
        return e.toString();
    } catch (JSONException e) {
        e.printStackTrace();
        return e.toString();
    }

My php code is this:

$content = file_get_contents("php://input");
if(strcasecmp($_SERVER['REQUEST_METHOD'], 'POST') != 0)
{
   throw new Exception('Request method must be POST!');
}

//Make sure that the content type of the POST request has been set to 
application/json

$contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : '';
if(strcasecmp($contentType, 'application/json') != 0){
    throw new Exception('Content type must be: application/json');
}

//Receive the RAW post data.
$content = trim(file_get_contents("php://input"));

//Attempt to decode the incoming RAW post data from JSON.
$decoded = json_decode($content, true);
echo($decoded);
exit;

The $decoded is null when I make a request using my Android application and using json_last_error() function I get JSON_ERROR_SYNTAX.

This is the raw content of the post request:

{"name":"test","identifier":"12345677"}

But I can’t understand what is the problem. In fact when I try to use Advance Rest Client to simulate the same request it works perfectly as shown in the picture below.

enter image description here

Answers:

I finally solved my Problem.. It seems to be the

URLEncoder.encode(json.toString(),"UTF-8"); 

In fact removing it and sending just output.writeBytes(json.toString());
Everything works perfectly