Android JSON 解析,第一个值永久存储


Android JSON parsing, first value stored permanently

我为我的安卓应用程序使用 Json 解析。我的本地主机中有 10 多个 php 文件,它们在我的 android 应用程序中以具有相应名称的列表视图形式给出。当我单击第一个时,应用程序将按名称访问第一个 php 文件。 JSON 解析适用于第一个 php 文件。描述,图像和视频变得流畅。但是当我单击第二个("kannur"(或任何其他项目时,第二个 php 文件被执行,但它仍然搜索第一个(kasargod(。

日志猫截图附在末尾。

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

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

ilovepjs==> KEY_ID 只是连续的数字,新字符串包含在列表视图中进行的相应点击,值为 Kasargod、kannur 等(这些是印度喀拉拉邦的地区(。php 文件的名称相同。而且没有错误(我得到描述,所有地区的图像,但只有我单击的第一个返回值,当我返回并单击列表视图中的其他某个地区时,没有任何反应!您可以从随附的Logcat屏幕截图中了解问题。

您的程序在以下两行之一上崩溃

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

因为它试图找到具有kannur价值的东西,所以我假设是你的KEY_ID

添加一些 printlns/log 消息,并确保KEY_IDKEY_DISTRICT包含您想要的内容。