android - com.google.gson.JsonSyntaxException when AsyncTask with Parser is called More than once -
i'm using asynctask retrieve data backend server , parsing using gson parser. i'm getting com.google.gson.jsonsyntaxexception when execute asynctask more once
this parser async
public class parsetask extends asynctask<string, void, jsonobject> { public jsonobject jsonobject; string url; @override protected jsonobject doinbackground(string... arg0) { url = arg0[0]; try { url url = new url(url); httpurlconnection connection = (httpurlconnection) url.openconnection(); connection.connect(); jsonparser parser = new jsonparser(); jsonobject = parser.parse(new inputstreamreader((inputstream) connection.getcontent())).getasjsonobject(); } catch (exception e) { e.printstacktrace(); } return jsonobject; } @override protected void onpostexecute(jsonobject json) { super.onpostexecute(json); }
}
this main activity
public class drillonefragment extends activity { private static string url = "abc"; private static final string tag_year = "year"; layoutparams params; view view; private jsonobject result; string q; string[] qq = { "2013", "2014" }; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.a); (int = 0; < 2; i++) { list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair(tag_year, qq[i])); string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; try { result = new parsetask().execute(url).get(); log.e(qq[i], result.tostring()); } catch (exception e) { e.printstacktrace(); } } }
}
this logcat
08-19 17:14:51.937: w/system.err(14735): com.google.gson.jsonsyntaxexception: com.google.gson.stream.malformedjsonexception: use jsonreader.setlenient(true) accept malformed json @ line 1 column 6 path $ 08-19 17:14:51.937: w/system.err(14735): @ com.google.gson.jsonparser.parse(jsonparser.java:65) 08-19 17:14:51.937: w/system.err(14735): @ com.example.tester.parsetask.doinbackground(parsetask.java:25) 08-19 17:14:51.937: w/system.err(14735): @ com.example.tester.parsetask.doinbackground(parsetask.java:1) 08-19 17:14:51.937: w/system.err(14735): @ android.os.asynctask$2.call(asynctask.java:288) 08-19 17:14:51.937: w/system.err(14735): @ java.util.concurrent.futuretask.run(futuretask.java:237) 08-19 17:14:51.937: w/system.err(14735): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) 08-19 17:14:51.937: w/system.err(14735): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) 08-19 17:14:51.937: w/system.err(14735): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) 08-19 17:14:51.937: w/system.err(14735): @ java.lang.thread.run(thread.java:841) 08-19 17:14:51.937: w/system.err(14735): caused by: com.google.gson.stream.malformedjsonexception: use jsonreader.setlenient(true) accept malformed json @ line 1 column 6 path $ 08-19 17:14:51.937: w/system.err(14735): @ com.google.gson.stream.jsonreader.syntaxerror(jsonreader.java:1573) 08-19 17:14:51.937: w/system.err(14735): @ com.google.gson.stream.jsonreader.checklenient(jsonreader.java:1423) 08-19 17:14:51.937: w/system.err(14735): @ com.google.gson.stream.jsonreader.dopeek(jsonreader.java:546) 08-19 17:14:51.937: w/system.err(14735): @ com.google.gson.stream.jsonreader.peek(jsonreader.java:429) 08-19 17:14:51.937: w/system.err(14735): @ com.google.gson.jsonparser.parse(jsonparser.java:60) 08-19 17:14:51.937: w/system.err(14735): ... 8 more 08-19 17:14:51.937: w/system.err(14735): java.lang.nullpointerexception 08-19 17:14:51.937: w/system.err(14735): @ com.example.tester.drillonefragment.oncreate(drillonefragment.java:41) 08-19 17:14:51.937: w/system.err(14735): @ android.app.activity.performcreate(activity.java:5248) 08-19 17:14:51.937: w/system.err(14735): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) 08-19 17:14:51.937: w/system.err(14735): @ android.app.activitythread.performlaunchactivity(activitythread.java:2162) 08-19 17:14:51.937: w/system.err(14735): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2247) 08-19 17:14:51.937: w/system.err(14735): @ android.app.activitythread.access$800(activitythread.java:141) 08-19 17:14:51.937: w/system.err(14735): @ android.app.activitythread$h.handlemessage(activitythread.java:1210) 08-19 17:14:51.937: w/system.err(14735): @ android.os.handler.dispatchmessage(handler.java:102) 08-19 17:14:51.937: w/system.err(14735): @ android.os.looper.loop(looper.java:136) 08-19 17:14:51.937: w/system.err(14735): @ android.app.activitythread.main(activitythread.java:5050) 08-19 17:14:51.937: w/system.err(14735): @ java.lang.reflect.method.invokenative(native method) 08-19 17:14:51.937: w/system.err(14735): @ java.lang.reflect.method.invoke(method.java:515) 08-19 17:14:51.937: w/system.err(14735): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 08-19 17:14:51.937: w/system.err(14735): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:609) 08-19 17:14:51.937: w/system.err(14735): @ dalvik.system.nativestart.main(native method)
thanks in advance.
you misused call asynctask class, must instantiate class , define parameters, connect , json objects. remember use motodo onpostexecute () output. may not
result = new parsetask().execute(url).get();
you must following. code works fine me
private class getarticulosparam extends asynctask<string, void, void> { public getarticulosparam() { contactlist = new arraylist<hashmap<string, string>>(); } @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(mainactivity.this); pdialog.setmessage("por favor, espere..."); pdialog.setcancelable(true); pdialog.show(); } @override protected void doinbackground(string... arg0) { // string // url1="http://pracsysonline.dyndns.org/listaarticulos/consultaparam.php"; string url1 = "http://"+direccion+"/listaarticulos/consultaparam.php"; string param = arg0[0]; list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("param", param)); // creating service handler class instance servicehandler sh = new servicehandler(); // making request url , getting response string jsonstr =sh.makeservicecall(url1,servicehandler.post,params); log.d("response: ", "> " + jsonstr); if (jsonstr != null) { try { // getting json array node contacts = new jsonarray(jsonstr); // looping through contacts (int = 0; < contacts.length(); i++) { jsonobject c = contacts.getjsonobject(i); string id = c.getstring(tag_desc); string name = c.getstring(tag_code); hashmap<string, string> contact = new hashmap<string, string>(); log.e("prueba", img); // adding each child node hashmap key => value contact.put(tag_desc, id); contact.put(tag_code, name); contactlist.add(contact); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); miadapter = new listviewadapter(mainactivity.this, contactlist); setlistadapter(miadapter); pdialog.dismiss(); } }
then class servicehandler
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) { urlencodedformentity encodedentity = new urlencodedformentity(params,"utf-8"); encodedentity.setcontentencoding(http.utf_8); httppost.setentity(encodedentity); //httppost.setentity(new urlencodedformentity(params,"utf-8")); } 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); } //codigo pruebna response = new scanner(httpresponse.getentity().getcontent(),"utf-8").usedelimiter("\\a").next(); /*httpentity = httpresponse.getentity(); response = entityutils.tostring(httpentity,"utf-8");*/ } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return response; }
}
Comments
Post a Comment