In my Android app I am loading json data with a Volley JsonArrayRequest
. The data were created by myself and I saved them with Sublime with UTF-8 encoding. When I get the Response
and fill my ListView
, the texts are not displayed correctly (umlauts). This is what my Request looks like:
JsonArrayRequest request = new JsonArrayRequest(targetUrl,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(final JSONArray response) {
try {
fillList(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue.add(request);
When I load the exact same data with this method, all texts are displayed correctly:
final StringBuilder builder = new StringBuilder();
final HttpClient client = new DefaultHttpClient();
final HttpGet httpGet = new HttpGet(request);
try {
final HttpResponse response = client.execute(httpGet);
final StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
final HttpEntity entity = response.getEntity();
final InputStream content = entity.getContent();
final BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
So to me it seems like there is no problem with the encoding of my json file. How can I fix this encoding problem in Volley?
I had the same problem earlier two days ago and i have tried every thing my friends just said and it`s the same problem and i tried so meany things .. my file is written in php and using volley to get the data as json i followed these steps to solve the problem ..
before executing the query in your database write this query
mysqli_query($this->connection,"set_NAMES_utf8");
use this header in your file if it`s php
header('Content-Type: application/json ; charset=utf-8 ');
while you are echoing the json according to php there is a solve for this problem use this special charachter in
json_encode($json,JSON_UNESCAPED_UNICODE)
i hope it helped
donot use try{} catch in the onResponse block, that is giving some problem in my code , rather than that you can implement like this .
and i think you will get the required result
I have same problem like this and i solve it using UTF-8 charset.
I hope this will work for you
If you know that absolutely all of the files you are requesting will be in the UTF-8 format, which it sounds like you do, then you might consider forcing your Volley request to return UTF-8 formatted strings. You could accomplish this by subclassing the standard JSON request. Something like this:
another approach: you can encode your whole response by this line of code.
I have encoded whole response string into UTF-8 as I know volley default encode method is iso8859-1
To specify in server part set utf-8 in content type like "text/html;charset=utf-8"