Skip to content Skip to sidebar Skip to footer

Expecting A String But Was BEGIN_OBJECT: JsonReader

There are some other answers to this question on here but not sure how to get them to work with my code. I have a lot of JSON to pass so I need to stream it rather than do it all

Solution 1:

The recommended way to do your http request is as follows:

URL url = new URL(sUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();


String sResult = null;
InputStream in = new BufferedInputStream(conn.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
try 
{
    StringBuilder out = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        out.append(line);
    }
    sResult = out.toString();
}   
finally 
{
    // Do cleanup
    try{
        in.close();
        reader.close();
    } catch (IOException e) {
        Log.wtf(AsyncRestHelper.class.getSimpleName(), "Could not close Input Stream!");
        e.printStackTrace();
    }
}

You can then use sResult to parse into Customers with GSON as you're currently doing


Post a Comment for "Expecting A String But Was BEGIN_OBJECT: JsonReader"