Note that there are some explanatory texts on larger screens.

plurals
  1. POJSON Object Parsing Error
    primarykey
    data
    text
    <p>I have got the following string as HTTPResponse. It is in JSON format.</p> <pre><code> [ { "From":"en", "OriginalTextSentenceLengths":[ 5 ], "TranslatedText":"Hallo", "TranslatedTextSentenceLengths":[ 5 ] }, { "From":"en", "OriginalTextSentenceLengths":[ 8 ], "TranslatedText":"Frage", "TranslatedTextSentenceLengths":[ 5 ] }, { "From":"en", "OriginalTextSentenceLengths":[ 6 ], "TranslatedText":"Antwort", "TranslatedTextSentenceLengths":[ 7 ] } ] </code></pre> <p>So this string I am parsing as follows to get the "Translated Texts array"</p> <pre><code> String resp = "[{\"From\":\"en\",\"OriginalTextSentenceLengths\":[5],\"TranslatedText\":\"Hallo\",\"TranslatedTextSentenceLengths\":[5]},{\"From\":\"en\",\"OriginalTextSentenceLengths\":[8],\"TranslatedText\":\"Frage\",\"TranslatedTextSentenceLengths\":[5]},{\"From\":\"en\",\"OriginalTextSentenceLengths\":[6],\"TranslatedText\":\"Antwort\",\"TranslatedTextSentenceLengths\":[7]}]"; String[] stringArray = null; try { JSONArray finalResult=null; JSONTokener tokener = new JSONTokener(resp); finalResult = new JSONArray(tokener); stringArray = new String[finalResult.length()]; for(int i=0;i&lt;finalResult.length();i++){ JSONObject e = finalResult.getJSONObject(i); Log.v("TAG",e.getString("TranslatedText")); stringArray[i]=e.getString("TranslatedText"); } }catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>I am getting the extracted "translated text" array ( Hallo, Frage, Antwort) out of the JSON Object...</p> <p>But when I am doing the same procedure by giving the same string as input to JSONTokener directly i.e after getting HttpResponse as below, I am <strong>getting JSON Exception at finalResult = new JSONArray(tokener) line....</strong> </p> <p><strong>org.json.JSONException: End of input at character 0 of</strong></p> <pre><code>String resp = getHttpResponse(uri); String[] stringArray = null; try { JSONArray finalResult=null; JSONTokener tokener = new JSONTokener(resp); finalResult = new JSONArray(tokener); stringArray = new String[finalResult.length()]; for(int i=0;i&lt;finalResult.length();i++){ JSONObject e = finalResult.getJSONObject(i); Log.v("TAG",e.getString("TranslatedText")); stringArray[i]=e.getString("TranslatedText"); } }catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } </code></pre> <p>I have tried hard for 2 days to resolve this error but couldn't do it.. So I am posting it here... Please help</p> <p><strong>EDIT:</strong></p> <p>I am adding the implementation of getHttpResponse</p> <pre><code> public static String getHttpResponse(URI uri) { Log.d("APP_TAG", "Going to make a get request"); StringBuilder response = new StringBuilder(); try { HttpGet get = new HttpGet(); get.setURI(uri); //DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams httpParameters = new BasicHttpParams(); int timeoutConnection = 30000; HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); int timeoutSocket = 30000; HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters); Log.v("TAG","1"); HttpResponse httpResponse = httpClient.execute(get); Log.v("TAG","2"); if (httpResponse.getStatusLine().getStatusCode() == 200) { Log.d("demo", "HTTP Get succeeded"); HttpEntity messageEntity = httpResponse.getEntity(); InputStream is = messageEntity.getContent(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { response.append(line); } } } catch (Exception e) { Log.e("demo", e.getMessage()); } Log.d("demo", "Done with HTTP getting"); return response.toString(); } </code></pre> <p>And the uri which I am giving to getHttpResponse is as follows</p> <pre><code> String[] texts = {"hello","question","answer"}; final String params = "appId=" + URLEncoder.encode("78280AF4DFA1CE1676AFE86340C690023A5AC139","UTF-8") + "&amp;from=" + URLEncoder.encode("en","UTF-8") + "&amp;to=" + URLEncoder.encode("de","UTF-8") + "&amp;texts=" + URLEncoder.encode(buildStringArrayParam(texts),"UTF-8"); final URL url = new URL("http://api.microsofttranslator.com/V2/Ajax.svc/TranslateArray?" + params); URI myURI = java.net.URI.create(url.toString()); String resp = getHttpResponse(myURI); </code></pre> <p>This response string is what I am trying to parse...</p> <p>This is for buildStringArrayParam(texts) </p> <pre><code>StringBuilder targetString = new StringBuilder("[\""); String value; for(Object obj : values) { if(obj!=null) { value = obj.toString(); if(value.length()!=0) { if(targetString.length()&gt;2) targetString.append(",\""); targetString.append(value); targetString.append("\""); } } } targetString.append("]"); return targetString.toString(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload