I have only one httpclient in my 1st activity which is used in all other activities.Because I am using PHP Sessions.
In my 1st Activity I have a listview which takes 4-5 secs to load items
(which will be done by a connection to the server), and in the same activity I have a search field…which on button click takes me to a searchActivity where using same httpclient search results will be loaded in a different listview.
My problem is during the 4-5 sec load time in the 1st activity if I try to search something, my app crashes saying:
Invalid use of SingleClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
java.lang.IllegalStateException: No wrapped connection.
and eventually a null pointer exception later
I think as I am using the same httpclient in the searchActivity before completing the 1st Activity is creating this error (correct me if am wrong)
so if my assumption is right how can i release this connection in the intent where i am moving to searchAvtivity from 1st activity??
Thank you
CODE:
try {
HttpPost httppost = new HttpPost(
SignUpActivity.url+"/feed/fetchfeed");
httppost.setHeader("X_REQUESTED_WITH", "xmlhttprequest");
httppost.setHeader("MOBILE_DATA_REQUESTED", "mobileHttpRequest");
HttpResponse response = SignUpActivity.httpclient.execute(httppost);
Log.d("response", "" + response);
HttpEntity entity = response.getEntity();
Log.d("entity", "" + entity);
is = entity.getContent();
Log.d("is", "" + is);
try {
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
Log.d("sb", "" + sb);
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
Log.d("result", result);
} catch (Exception e) {
Log.e("error3", "Error in http connection" + e.toString());
}
Log.e("response", "response is:" + response.toString());
} catch (Exception e) {
Log.e("error4", "Error in http connection" + e.toString());
}
try {
Log.d("JArray", "entered try");
JArray = new JSONArray(result);
feed_products_list = new ArrayList<Product>();
Log.d("JArray", "try last line");
} catch (JSONException e) {
Log.d("JArray", "in catch");
e.printStackTrace();
}
JSONObject jsonObj;
JSONObject jsonObjStore;
JSONObject jsonObjUser;
for (int i = 0; i < JArray.length(); i++) {
try {
Log.d("jsonObj",
"entered try" + " " + i + " " + JArray.length());
jsonObj = JArray.getJSONObject(i);
jsonObjUser = jsonObj.getJSONObject("user");
} catch (Exception e) {
Log.d("jsonObj", "in catch");
continue;
}
try {
Log.d("feed_products_list", "entered try");
Log.d("type of action", jsonObj.getString("action"));
if (jsonObj.getString("action").equals("entry")) {
Log.d("if block", "entered block 'entry'");
jsonObjStore = jsonObj.getJSONObject("store");
Log.d("store", jsonObjStore.getString("name"));
feed_products_list.add(new Product(jsonObj.getInt("id"),
jsonObj.getString("action"), jsonObj
.getString("image"), jsonObj
.getString("product_name"), jsonObj
.getString("reported_price_formated"),
jsonObjStore.getString("name"), jsonObjStore
.getString("area"), jsonObjStore
.getString("city"), jsonObj
.getString("mrp"), jsonObjUser
.getString("name"), jsonObj
.getLong("reported_timestamp"), jsonObj
.getInt("discount"), jsonObjStore
.getDouble("lat"), jsonObjStore
.getDouble("lng")));
Log.d("store id", jsonObjStore.getString("id"));
Log.d("feed_products_list product: ",
feed_products_list.get(i).lat + " "
+ feed_products_list.get(i).lng);
} else if (jsonObj.getString("action").equals("contest")) {
Log.d("if block", "entered block 'contest'");
feed_products_list.add(new Product(jsonObj.getInt("id"),
jsonObj.getString("action"), jsonObjUser
.getString("image"), jsonObj
.getString("product_name"), jsonObj
.getString("city"), jsonObjUser
.getString("name"), jsonObj
.getLong("reported_timestamp")));
Log.d("feed_products_list",
feed_products_list.get(i).productName);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
m = 1;
}
I added this:
try {
is.close();
Intent intent = new Intent(FeedListViewActivity.this,
SearchActivity.class);
startActivity(intent);
Log.d("onClick", search_string);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
but i got the same error
Connection are automatically closed if you close the
InputStream‘s you get from httpclient. Check your code and make sure you are closing them properly.