We are doing lots of ( All necessary ) call on server to get user data, For this we created a common class to post data, and retrive response as JSON object. But by this our application become too slow, So we are thinking, we can retrieve data via async task.
So we modifed our code like below:-
UserFunction.java
public JSONObject getUserData(String userName,String pNumber){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", getUser));
params.add(new BasicNameValuePair("search_number_array", userName));
params.add(new BasicNameValuePair("pNumber", pNumber));
params.add(new BasicNameValuePair("id", pNumber));
// getting JSON Object
JSONParser jsonParser1=new JSONParser(loginURL, params);
// jsonParser1.getJSONFromUrl(loginURL, params);
jsonParser1.execute();
Jsonresult j1=new Jsonresult();
JSONObject json =j1.getjObjResult();
Log.d("Response:", json.toString()); //line 45, on which getting error
return json;
}
JSONParse.java
public class JSONParser extends AsyncTask<String, String, JSONObject> {
static InputStream is = null;
static JSONObject jObj = null;
Jsonresult obj;
static String json = "";
List<NameValuePair> postparams= new ArrayList<NameValuePair>();
String URL=null;
// constructor
public JSONParser(String url, List<NameValuePair> params) {
URL=url;
postparams=params;
Log.e("entered constructor", "entered in constructor ");
// Making HTTP request
}
@Override
protected JSONObject doInBackground(String... strings) {
Log.e("entered in bg", "entered in doinbackground ");
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
httpPost.setEntity(new UrlEncodedFormEntity(postparams));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = 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 the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
public void getJSONFromUrl(String url, List<NameValuePair> params) {
URL=url;
postparams=params;
Log.e("entered ", "entered in getjsonurl ");
// Making HTTP request
}
@Override
protected void onPostExecute(JSONObject result) {
this.obj.setjObjResult(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
}
Jsonresult.java
public class Jsonresult {
JSONObject jObjResult = null;
public JSONObject getjObjResult() {
return jObjResult;
}
public void setjObjResult(JSONObject jObjResult) {
this.jObjResult = jObjResult;
}
}
But we are getting this error
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
at com.keepAeye.gps.UserFunctions.getUserData(UserFunctions.java:45)
Means control is going before finishing background process. Please tell how we can fix this?