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:

URLurl=newURL(sUrl);
HttpURLConnectionconn= (HttpURLConnection) url.openConnection();


StringsResult=null;
InputStreamin=newBufferedInputStream(conn.getInputStream());
BufferedReaderreader=newBufferedReader(newInputStreamReader(in));
try 
{
    StringBuilderout=newStringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {
        out.append(line);
    }
    sResult = out.toString();
}   
finally 
{
    // Do cleanuptry{
        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"