0

I am running an asyncTask to retrieve a JSON array of objects from a HttpPost.

The output from the webservice is:

  [
    {
        "id": 1,
        "question": "What town were you born in?"
    },
    {
        "id": 2,
        "question": "What is your mothers maiden name?"
    },
    {
        "id": 3,
        "question": "Who was your childhood hero?"
    }
]

But get the JSONException:

org.json.JSONException: End of input at character 0 of

Which refers to JArray in the following code:

JSONArray JArray;

Context ra;

public SecurityQuestionsTask(Context _registerActivity) {
    ra = _registerActivity;
    JArray = new JSONArray();
}

@Override
protected Long doInBackground(String... langs) {

    Long result = 0L;

    String lang = Locale.getDefault().getLanguage();

    String[] langArray = {"en", "de", "fr"};

    if (!Arrays.asList(langArray).contains(lang)) {
        lang = "en";
    }

    try {

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://webservice.com/api/v1/securityquestions?lang=" + lang);

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            JSONString = EntityUtils.toString(response.getEntity(), "UTF-8");
            JArray = new JSONArray(JSONString);

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }



    publishProgress("Retrieving security questions...");

    return result;
}

Am I parsing the response to a JSONArray correctly?

edit:

This is how the webservice responds:

return Response.status(200).entity(json.toString()).build();
crmepham
  • 4,676
  • 19
  • 80
  • 155
  • 3
    Is EntityUtils.toString(response.getEntity(), "UTF-8"); returning the expected JSON string? And is it assigned to a variable that is passed to JSONArray constructor? – nomis Mar 02 '15 at 12:18
  • 1
    Share same response which getting in `JSONString` because exception saying trying to convert invalid string to JSONArray or JSONObject – ρяσѕρєя K Mar 02 '15 at 12:20
  • 1
    Did you try to input your expected data to `JSONString` variable like `JSONString = "[{\"id\": 1,\"question\": \"What town were you born in?\"}]"`? It is required to diagnose whether your error is from http communication or content parsing. – Youngjae Mar 02 '15 at 12:26
  • @Youngjae I just did this and it is not building the `JSONString` from response. – crmepham Mar 02 '15 at 12:30
  • The webserver converts a `JSONArray` to a `String` before sending it as a `Response` entity. Is this correct? – crmepham Mar 02 '15 at 12:32
  • @crm // webserver can convert data to Json string before making a Response entity or can _serialize_ at the end of http output. It depends on the server application design, configuration, or response handling so it cannot say correct or not. – Youngjae Mar 02 '15 at 12:38
  • @crm // would you please check this link? http://stackoverflow.com/q/10684086/361100 seems duplicate question. – Youngjae Mar 02 '15 at 12:41

2 Answers2

0

The clue was in the question title it seems!

I had to use HttpGet instead of HttpPost with the way I have my web service setup.

so this line:

HttpPost httppost = new HttpPost("http://webservice.com/api/v1/securityquestions?lang=" + lang);

had to change to this:

HttpGet httpGet = new HttpGet("http://webservice.com/api/v1/securityquestions?lang=" + lang);
crmepham
  • 4,676
  • 19
  • 80
  • 155
-1

If the String response is like the one you posted, try

     // Execute HTTP Post Request
     HttpResponse response = httpclient.execute(httppost);
     String resp = EntityUtils.toString(response.getEntity(), "UTF-8");
     JSONArray jsonArr = new JSONArray(resp);
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
  • I need the JSONArray to be a class object so I can use it to populate a list in the `onPostExecute` method of an `AsycTask`. – crmepham Mar 02 '15 at 12:55
  • `jsonArray` can be a object member, no problem, just remove the declaration `JSONArray` in front and declare `jsonArr` in the class like you actually do. – T.Gounelle Mar 02 '15 at 13:11