i have utf-8 json file.
how can print utf-8 support?
output = (textview) view.findviewbyid(r.id.jdata); jsonobjectrequest jor = new jsonobjectrequest(request.method.get, loginurl, null, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { try{ jsonarray ja = response.getjsonarray("posts"); for(int i=0; < ja.length(); i++){ jsonobject jsonobject = ja.getjsonobject(i); string title = jsonobject.getstring("title"); string text = jsonobject.getstring("text"); data += "blog number "+(i+1)+" \n title= "+title +" \n text= "+ text +" \n\n\n\n "; } output.settext(data); }catch(jsonexception e){e.printstacktrace();} } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e("volley","error"); } } ); requestqueue.add(jor);
the data (json file) created myself , saved them utf-8 encoding:
{ "found": 2, "posts": [{ "title": "title", "text": "text" }, { "title": "title", "text": "text" }] }
i changed android file encoding settings on utf-8, nothing changed. how fix problem?
you can overriding parsenetworkresponse
method of jsonobjectrequest class. first make new class extends jsonobjectrequest:
public class utf8jsonobjectrequest extends jsonobjectrequest { public utf8jsonobjectrequest(int method, string url, response.listener<jsonobject> listener, response.errorlistener errorlistener) { super(method, url, listener, errorlistener); } @override protected response<t> parsenetworkresponse(networkresponse response) { try { string json = new string( response.data, "utf-8" ); return response.success( new jsonobject(json), httpheaderparser.parsecacheheaders(response) ); } catch (unsupportedencodingexception e) { return response.error(new parseerror(e)); } catch (jsonsyntaxexception e) { return response.error(new parseerror(e)); } } }
then use new class in place of jsonobjectrequest:
utf8jsonobjectrequest jor = new utf8jsonobjectrequest( request.method.get, loginurl, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { //... } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { log.e("volley","error"); } } );
Comments
Post a Comment