I\'ve spent 2 days to find a solution with of problem.
Here is the error:
E/log_tag: Error parsing data org.json.JSONException: Value of type java.l
JSON data should always be encoded in UTF-8 (and your data most likely is). However, you read it using the ISO-8859-1. Since your JSON data contains Cyrillic characters, they will lead to invalid data that can no longer be parsed as JSON. (In UTF-8, Cyrillic characters required two bytes. However, ISO-8859-1 treats each byte as a separate character.)
The fix is to use the correct encoding, most likely UTF-8:
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
Update:
The code you're using is posted all over the Internet, sometimes with ISO-8859-1 and sometimes with UTF-8. If you stick to the standards, JSON with ISO-8859-1 encoding is invalid.
The Android JSON parser is very unfortunate because it only supports strings as a parsing source. As a result, the complex code you're using is required to parse a JSON HTTP response. Memory and processing wise, it's pretty inefficient. It would be nice if Google would extend the JSON parser to directly work on an InputStream
instance without taking the detour via a huge String
instance.
Update 2:
I just realized that Android (starting with API level 11) contains a JSON parser working on InputStream
. However, it's in a different package and only produces a stream of tokens, not JSONObject or JSONArray instances. I guess I'll write the missing code myself to come up with an easy to use and efficient JSON parser (producing JSONObject or JSONArray instances).
Update 3:
If the JSON text to parse is invalid, Android throw an exception containing the message:
Value [{ "Id": "5207fc6473516724343ce7a5", "Name": "Эриван", ... }] of type java.lang.String cannot be converted to JSONArray
Since there is not JSON data between the word Value and of in your message, I suspect that your JSON data is in fact empty.
In your code, you should first check the status code of the HTTP response (getStatusLine().getStatusCode()
). Furthmore, I seems strange that you use a POST request without posting any data. Shouldn't you use a GET request?
The problem is that your JSON is in the incorrect format. I have tried with your sample JSON and found the solution to it. Now the inbuilt JSONObject and JSONArray cannot be used to get such a json response.
You need to add json-simple library to your project by adding it to gradle:
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
Or download the library "json-simple-1.1.1.jar" from this link https://repo1.maven.org/maven2/com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar
Then you can parse your JSON easily and it won't give any error. I have made a small sample code for you on how to use it :
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
JSONParser parser_obj = new JSONParser();
JSONArray array_obj = (JSONArray) parser_obj.parse("String from web service");
// in your case it will be "result"
Then you can process it as per your need.
Your Json response seems just fine. However its not always guaranteed that the response string is always filled up with data. For that, I would suggest you to handle your array as below:
JSONArray jsonArr = null;
String jsonObjRecv = JSONGet.getJSONfromURL(URL_LIST);
if(!TextUtils.isEmpty(jsonObjRecv)){
jsonArr = new JSONArray(jsonObjRecv);
}
else{
Log.w("json", "jsonObjRecv is null");
}
In my case I was not decoding the jSon string before parsing it as jSon object. The following worked for me.
JSONObject responseJSonObj = new JSONObject( URLDecoder.decode( result, "UTF-8" ) );
Where result
contains the jSon string to be parsed.