java - null pointer exception error in http POST request JSON object -
i doing android coding , sending http post request using httpclient. , giving me null point expception error @ line
httpentity = httpresponse.getentity();
i used log cat , found code stopping @ line mentioned above. please me out it. in advance.
my code establishment of http client in file servicehandler:
package com.example.manumaheshwari.vigo; /** * created manumaheshwari on 30/06/15. */ import android.util.log; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import java.io.unsupportedencodingexception; import java.util.arraylist; import java.util.list; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.client.utils.urlencodedutils; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.apache.http.protocol.http; import org.apache.http.util.entityutils; public class servicehandler { static string response = null; public final static int = 1; public final static int post = 2; stringbuilder sb; 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); httppost.setheader(http.content_type, "application/x-www-form-urlencoded "); if (params != null) { try { httppost.setentity(new urlencodedformentity(params, "utf-8")); } catch (unsupportedencodingexception e) { // writing error log e.printstacktrace(); } // making http request try { httpresponse response = httpclient.execute(httppost); // writing response log log.d("http response: ", response.tostring()); } catch (clientprotocolexception e) { // writing exception log e.printstacktrace(); } catch (ioexception e) { // writing exception log e.printstacktrace(); } } } 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 code main activity follows:
package com.example.manumaheshwari.vigo; import android.app.listactivity; import android.app.progressdialog; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.menuitem; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import org.apache.http.namevaluepair; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; import java.util.list; public class mainactivity extends listactivity { private progressdialog pdialog; // url pending rides json private static string url = "http://128.199.206.145/vigo/v1/displayalldrivers"; // json node names private static final string tag_source = "source"; private static final string tag_destination = "destination"; private static final string tag_driver_id = "driver_id"; private static final string tag_name = "name"; // pending rides jsonarray jsonarray pendingrides = null; // hashmap listview arraylist<hashmap<string, string>> pendingrideslist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); pendingrideslist = new arraylist<hashmap<string, string>>(); listview lv = getlistview(); // calling async task json new getrides().execute(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } /** * async task class json making http call * */ private class getrides extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); // showing progress dialog pdialog = new progressdialog(mainactivity.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(); list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("contractor_id", "1")); // making request url , getting response string jsonstr = sh.makeservicecall(url, servicehandler.post, namevaluepairs); log.d("response: " , "> " + jsonstr); if (jsonstr != null) { try { jsonobject jsonobj = new jsonobject(jsonstr); // getting json array node pendingrides = jsonobj.getjsonarray("driver"); // looping through contacts (int = 0; < pendingrides.length(); i++) { jsonobject c = pendingrides.getjsonobject(i); string source = c.getstring(tag_name); string destination = c.getstring(tag_driver_id); // tmp hashmap single contact hashmap<string, string> pendingride = new hashmap<string, string>(); // adding each child node hashmap key => value pendingride.put(tag_name, source); pendingride.put(tag_driver_id, destination); // adding pending ride pending ride list pendingrideslist.add(pendingride); } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("servicehandler", "couldn't data url"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog if (pdialog.isshowing()) pdialog.dismiss(); /** * updating parsed json data listview * */ listadapter adapter = new simpleadapter( mainactivity.this, pendingrideslist, r.layout.list_view, new string[] { tag_source, tag_destination,}, new int[] { r.id.source,r.id.destination}); setlistadapter(adapter); } } }
it seems httpresponse
null,because in post method didn't assign value it.so in post method,change
httpresponse response = httpclient.execute(httppost); // writing response log log.d("http response: ", response.tostring());
to
httpresponse = httpclient.execute(httppost); // writing response log log.d("http response: ", httpresponse.tostring());
Comments
Post a Comment