2013-10-08 25 views
0

我正在使用Json解析我的android應用程序。在我的本地主機中有超過10個php文件,這些文件在我的android應用程序中以列表視圖的形式給出,並帶有相應的名稱。當我點擊第一個時,應用程序將通過名稱訪問第一個php文件。 JSON解析適用於第一個php文件。說明,圖像和視頻正在順利進行。但是當我點擊第二個(「kannur」)或任何其他項目,第二個php文件被執行,但它仍然搜索第一個(kasargod)。Android JSON解析,永久存儲第一個值

Logcat截圖附在末尾。

public class CustomizedListView extends Activity { 


    // All static variables 

    // JSON node keys 
    public static int pos ; 
    static final String KEY_ROOT = DistrictActivity.dist; 
    static final String KEY_ID = "id"; 
    static final String KEY_DISTRICT = "district_name"; // parent n 
    static final String KEY_PLACE = "place_name"; 
    static final String KEY_DESCRIPTION = "description"; 
    public static final String KEY_IMAGE = "image_url"; 
    public static final String KEY_THUMB_URL = "thumnail"; 
    public static String address="http://10.0.2.2/tour/"; 

    GridView list; 
    LazyAdapter adapter; 
    JSONArray contacts; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Bundle extras = getIntent().getExtras(); 

      final String newString = extras.getString("STRING_I_NEED"); 


      final String URL = address+newString+".php"; 

      System.out.println(URL); 


     ArrayList<HashMap<String, String>> placesList = new ArrayList<HashMap<String, String>>(); 
     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 

     // getting JSON string from URL 



     JSONObject json = jParser.getJSONFromUrl(URL); 

     try { 
      // Getting Array of Contacts 
      contacts = json.getJSONArray(KEY_ROOT); 

      // looping through All Contacts 
      for(int i = 0; i < contacts.length(); i++){ 
       JSONObject c = contacts.getJSONObject(i); 

       // Storing each json item in variable 
       String id = c.getString(KEY_ID); 
       String district = c.getString(KEY_DISTRICT); 
       String place = c.getString(KEY_PLACE); 
       String thumnail = c.getString(KEY_THUMB_URL); 
       String image_url= c.getString(KEY_IMAGE); 


       // creating new HashMap 
       HashMap<String, String> map = new HashMap<String, String>(); 

       // adding each child node to HashMap key => value 
       map.put(KEY_ID, id); 
       map.put(KEY_DISTRICT, district); 
       map.put(KEY_PLACE, place); 
       map.put(KEY_THUMB_URL, thumnail); 
       map.put(KEY_IMAGE, image_url); 


       // adding HashList to ArrayList 
       placesList.add(map); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

screenshot->https://www.dropbox.com/s/8dd2l8qcj4lslsj/stack.png

ilovepjs ==> KEY_ID只是連續的數字,則包含newstring在列表視圖中與所述值Kasargod做了相應的點擊次數,坎努爾等(這些都是在各區印度喀拉拉邦)。 php文件名稱相同。並且沒有任何錯誤(我得到描述,所有區域的圖像,但只有第一個點擊時會返回值,當我返回並單擊列表視圖中的其他區域時,什麼都不會發生!)。您可以從附加的Logcat的屏幕截圖中瞭解問題。

回答

0

程序發生崩潰的位置上這兩條線

String id = c.getString(KEY_ID);  
String district = c.getString(KEY_DISTRICT); 

之一,因爲它正試圖找到一個有價值的東西了kannur我假設你KEY_ID

添加一些printlns /日誌消息並確保KEY_IDKEY_DISTRICT包含你想要他們應該。