2012-05-29 31 views
1

我只有一個httpclient在我的第一個活動中用於所有其他activities.Because我使用PHP會話發佈android中的連接

在我的第一個活動中,我有一個listview需要4-5秒來加載項目 (這將通過連接到服務器完成),並在同一活動中我有一個搜索字段...哪按鈕點擊將我帶到一個searchActivity,在這裏使用相同的httpclient搜索結果將被加載到不同的列表視圖中。 我的問題是過程中的第一次活動的4-5秒的加載時間,如果我試圖尋找的東西,我的應用程序崩潰說:

無效使用SingleClientConnManager的:連接仍然分配。

確保在分配另一個連接之前釋放連接。

java.lang.IllegalStateException:沒有包裹連接。

並最終導致空指針異常後

我想,我正在使用的searchActivity相同的HttpClient完成了第一次活動是創建這個錯誤之前(如果正確的話我是錯的)

所以如果我的假設是正確的我怎麼能釋放這個連接的意圖,我要從第一活動searchAvtivity?

謝謝

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; 
} 

我加了這一點:

   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(); 
       } 

,但如果你關閉InputStream'我得到了同樣的錯誤

回答

1

連接被自動關閉你從httpclient獲得。檢查你的代碼,並確保你正確關閉它們。

+0

你的意思是我需要在開始意圖之前關閉InputStream? – Housefly

+0

實際上現在我正在關閉所有需要的數據(在4-5秒的加載時間內)後關閉InputStream – Housefly

+0

顯示一些代碼。 –