Prince Ali Prince Ali - 3 months ago 53
JSON Question

W/System.err: Unexpected character () at position 0


I am trying to parse json response from this link:
https://www.move2mart.com/wp-json/wp/v2/pages?slug=watches
and this is what i am getting no matter what i do.
W/System.err: Unexpected character () at position 0.


public class MainActivity extends AppCompatActivity {

TextView textView;
String url="https://www.move2mart.com/wp-json/wp/v2/pages?slug=mens-fashion";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

textView= (TextView) findViewById(R.id.text);

final RequestQueue requestQueue = Volley.newRequestQueue(this);

StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

textView.setText(response);
JSONParser jsonParser = new JSONParser();

Log.d("Inside","1");
try {
Log.d("Inside","2");

org.json.JSONArray jsonArray = (org.json.JSONArray) jsonParser.parse(response);



try {
//org.json.JSONArray jsonObject=jsonArray.getJSONArray(0);
JSONObject jsonObject2 = (JSONObject) jsonParser.parse(new InputStreamReader(new FileInputStream(jsonArray.getString(0))));
String t1=jsonObject2.getString("id");
Log.d("Inside3",t1);
} catch (JSONException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


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


}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});

requestQueue.add(stringRequest);
}


}


Log Response: 08-15 08:40:46.266
18845-18845/com.example.princ.learningjsonvolly D/Inside: 1

08-15 08:40:46.266 18845-18845/com.example.princ.learningjsonvolly
D/Inside: 2

08-15 08:40:46.267 18845-18845/com.example.princ.learningjsonvolly
W/System.err: Unexpected character () at position 0.

08-15 08:40:46.268 18845-18845/com.example.princ.learningjsonvolly
W/System.err: at
org.json.simple.parser.Yylex.yylex(Yylex.java:610)

08-15 08:40:46.268 18845-18845/com.example.princ.learningjsonvolly
W/System.err: at
org.json.simple.parser.JSONParser.nextToken(JSONParser.java:269)

Answer

Seems like your response in gzip encoding, so let make GzipRequest:

public class GZipRequest extends StringRequest {

    public GZipRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method, url, listener, errorListener);
    }

    public GZipRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
    }

    // parse the gzip response using a GZIPInputStream
    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String output = ""; // note: better to use StringBuilder

        try {
            final GZIPInputStream gStream = new GZIPInputStream(new ByteArrayInputStream(response.data));
            final InputStreamReader reader = new InputStreamReader(gStream);
            final BufferedReader in = new BufferedReader(reader);
            String read;
            while ((read = in.readLine()) != null) {
                output += read;
            }
            reader.close();
            in.close();
            gStream.close();
        } catch (IOException e) {
            return Response.error(new ParseError());
        }
        return Response.success(output, HttpHeaderParser.parseCacheHeaders(response));
    }
}

And modify a little bit in your code:

GZipRequest stringRequest = new GZipRequest(Request.Method.GET, url, new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {

        textView.setText(response);

        try {
            org.json.JSONObject jsonObject = new org.json.JSONObject(response);

            String t1 = jsonObject.getString("id");

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


    }
}, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });