android - accessing JSON files, AsyncTask error -
there json
output:
{"status":"error","data":null,"message":"required parameters not available."}
to access there json parsing java class , user function java class.
i having confusion on accessing json
file on loginactivity inside asynctask. task throws null pointer exception. think not referring "status" causing ... real reason why ?? new android have no idea. please help. code follows:
jsonparser.java
public class jsonparser { static inputstream = null; static jsonobject jobj = null; static string json = ""; static int = '1'; static int post ='2'; // constructor public jsonparser() { } public jsonobject getjsonfromurl(string url, list<namevaluepair> params, int method) throws urisyntaxexception { defaulthttpclient httpclient = new defaulthttpclient(); httpresponse httpresponse = null; httpentity httpentity = null; try { switch (method) { case 1: if(params!=null){ string paramstring = urlencodedutils.format(params, "utf-8"); url += "?" + paramstring; } log.e("url", url); httpget httpget = new httpget(url); httpresponse = httpclient.execute(httpget); httpentity = httpresponse.getentity(); break; case 2: httppost postrequest = new httppost(url); postrequest.setentity(new urlencodedformentity(params)); httpresponse = httpclient.execute(postrequest); httpentity = httpresponse.getentity(); break; } = httpentity.getcontent(); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } try { bufferedreader reader = new bufferedreader(new inputstreamreader( is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); json = sb.tostring(); log.e("json", json); } catch (exception e) { log.e("buffer error", "error converting result " + e.tostring()); } // try parse string json object try { jobj = new jsonobject(json); } catch (jsonexception e) { log.e("json parser", "error parsing data " + e.tostring()); } // return json string return jobj; } }
userfunction.java
public class userfunctions { private jsonparser jsonparser; private static string url_login = "http://10.0.0.2/blamethestars2/public/api/user/login"; static int = '1'; static int post ='2'; // constructor public userfunctions(){ jsonparser = new jsonparser(); } public jsonobject login(string username, string password){ jsonobject json = null; list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("email", username)); params.add(new basicnamevaluepair("password", password)); try { json = jsonparser.getjsonfromurl(url_login, params, post); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } return json; } }
loginactivity.java
public class loginactivity extends activity{ edittext username, password; string user, pass; button submit; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.sign_in); username = (edittext)findviewbyid(r.id.user_email); password = (edittext)findviewbyid(r.id.user_password); submit = (button)findviewbyid(r.id.user_submit); submit.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub user = username.gettext().tostring(); pass = password.gettext().tostring(); if(user.length()<1 || pass.length()<1){ toast.maketext(getapplicationcontext(), "fill user name , pass", toast.length_short).show(); }else{ new asynconnection().execute(); } } }); } public class asynconnection extends asynctask<void, void, void>{ jsonobject jsarray; @override protected void doinbackground(void... params) { userfunctions users = new userfunctions(); jsarray = users.login(user, pass); return null; } } }
for trying check if api response appears on logcat or not
logcat error:
04-18 16:07:01.190: w/system.err(874): java.lang.nullpointerexception 04-18 16:07:01.260: w/system.err(874): @ com.example.btshome.library.jsonparser.getjsonfromurl(jsonparser.java:66) 04-18 16:07:01.260: w/system.err(874): @ com.example.btshome.library.userfunctions.login(userfunctions.java:38) 04-18 16:07:01.270: w/system.err(874): @ com.example.btshome.loginactivity$asynconnection.doinbackground(loginactivity.java:54) 04-18 16:07:01.270: w/system.err(874): @ com.example.btshome.loginactivity$asynconnection.doinbackground(loginactivity.java:1) 04-18 16:07:01.280: w/system.err(874): @ android.os.asynctask$2.call(asynctask.java:287) 04-18 16:07:01.280: w/system.err(874): @ java.util.concurrent.futuretask$sync.innerrun(futuretask.java:305) 04-18 16:07:01.280: w/system.err(874): @ java.util.concurrent.futuretask.run(futuretask.java:137) 04-18 16:07:01.293: w/system.err(874): @ android.os.asynctask$serialexecutor$1.run(asynctask.java:230) 04-18 16:07:01.293: w/system.err(874): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1076) 04-18 16:07:01.300: w/system.err(874): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:569) 04-18 16:07:01.320: w/system.err(874): @ java.lang.thread.run(thread.java:856)
Comments
Post a Comment