Trouble with UTF-8 JSON file used in android -


i'm working on android project , use json intechange data server.. problem here when use json file utf-8 format, crash happens , app stops.. , when don't, works fine. need have files in utf-8 format have correct charecters. missing? thank in advanced.

here service handler class:

public class servicehandler {

static string response = null; public final static int = 1; public final static int post = 2;  public servicehandler() { }  /**  * making service call  * @url - url make request  * @method - http request method  * */ public string makeservicecall(string url, int method) {     return this.makeservicecall(url, method, null); }  /**  * making service call  * @url - url make request  * @method - http request method  * @params - http request params  * */ public string makeservicecall(string url, int method, list<namevaluepair> params) {     try {         // http client         defaulthttpclient httpclient = new defaulthttpclient();         httpentity httpentity = null;         httpresponse httpresponse = null;          // checking http request method type         if (method == post) {             httppost httppost = new httppost(url);             // adding post params             if (params != null) {                 httppost.setentity(new urlencodedformentity(params));             }              httpresponse = httpclient.execute(httppost);          } else if (method == get) {             // appending params url             if (params != null) {                 string paramstring = urlencodedutils.format(params, "utf-8");                 url += "?" + paramstring;             }             httpget httpget = new httpget(url);             httpresponse = httpclient.execute(httpget);         }         httpentity = httpresponse.getentity();         response = entityutils.tostring(httpentity);      } catch (unsupportedencodingexception e) {         e.printstacktrace();     } catch (clientprotocolexception e) {         e.printstacktrace();     } catch (ioexception e) {         e.printstacktrace();     }      return response;  } 

and here method use in main activity:

private class getcontacts extends asynctask {

    @override     protected void onpreexecute() {         super.onpreexecute();         // showing progress dialog         pdialog = new progressdialog(secondactivity.this);         pdialog.setmessage("please wait...");         pdialog.setcancelable(false);         pdialog.show();      }      @override     protected void doinbackground(void... arg0) {         // creating service handler class instance         servicehandler sh = new servicehandler();          // making request url , getting response         string jsonstr = sh.makeservicecall(url, servicehandler.get);          log.d("response: ", "> " + jsonstr);          if (jsonstr != null) {             try {                 jsonobject jsonobj = null;                 try {                     jsonobj = new jsonobject(jsonstr);                 } catch (jsonexception e) {                     e.printstacktrace();                 }                  // getting json array node                 if (jsonobj != null) {                     contacts = jsonobj.getjsonarray(tag_contacts);                 }                   // looping through contacts                 (int = 0; < contacts.length(); i++) {                     jsonobject c = contacts.getjsonobject(i);                      string id = c.getstring(tag_id);                     string title = c.getstring(tag_title);                     string content = c.getstring(tag_content);                     string date = c.getstring(tag_date);                        // tmp hashmap single contact                     hashmap<string, string> contact = new hashmap<string, string>();                      // adding each child node hashmap key => value                     contact.put(tag_id, id);                     contact.put(tag_title, title);                     contact.put(tag_content, content);                     contact.put(tag_date, date);                      // adding contact contact list                     conatctlist.add(contact);                 }             } catch (jsonexception e) {                 e.printstacktrace();             }         } else {             log.e("servicehandler", "couldn't data url");         }          return null;     }      @override     protected void onpostexecute(void result) {         listview lv = (listview)findviewbyid(r.id.listview);         super.onpostexecute(result);         // dismiss progress dialog         if (pdialog.isshowing())             pdialog.dismiss();         /**          * updating parsed json data listview          * */         listadapter adapter = new simpleadapter(                 secondactivity.this, contactlist,                 r.layout.list_item_2, new string[] { tag_title, tag_content,                 tag_date }, new int[] { r.id.name,                 r.id.email, r.id.date });          lv.setadapter(adapter);      } }//end getcontacts 

may be, have define httpclient this...(not tested)

httpparams params = new basichttpparams(); httpprotocolparams.setversion(params, httpversion.http_1_1); httpprotocolparams.setcontentcharset(params, "utf-8"); params.setbooleanparameter("http.protocol.expect-continue", false); httpclient httpclient = new defaulthttpclient(params); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -