JSONException:没有polis的值

时间:2014-08-11 16:31:39

标签: android

在我的活动中,AsyncTask从数据库接收数组。问题是我不能"看到"数组内的值。

活动

    package com.example.tranfer;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;



public class DataInsert extends ListActivity {

    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String LOGIN_URL = "http://192.168.1.2:80/etruck1/data_insert.php";

    // JSON Node names

    JSONParser jsonParser = new JSONParser();


    // contacts JSONArray
    JSONArray contacts = null;

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

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

        contactList = new ArrayList<HashMap<String, String>>();


        new GetData().execute();
    }

    private class GetData extends AsyncTask<Void,String,JSONObject> {

         public void onPreExecute() {
                super.onPreExecute();
                Log.d("meg", "meg");

                pDialog = new ProgressDialog(DataInsert.this);
                pDialog.setMessage("Καταχωρώ τα στοιχεία...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();

            }

        protected JSONObject doInBackground(Void... args) {
            // TODO Auto-generated method stub
            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "GET", null);
            return json;
        }

        protected void onPostExecute(JSONObject json) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            Log.d("mega", json.toString());

            try {
                //JSONObject jObj = new JSONObject();
            JSONArray polis = json.getJSONArray("polis");
            for (int i = 0; i < polis.length(); i++) {
                JSONObject c = polis.getJSONObject(i);

                    String startpoli1 = c.getString("startPoli1");
                    String finalpoli1 = c.getString("finalpoli1");


                    HashMap<String, String> pinakas = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    pinakas.put("startpoli1", startpoli1);
                    pinakas.put("finalpoli1", finalpoli1);
                    Log.d("mega3", startpoli1);  
                    Log.d("mega4", finalpoli1);  

                    // adding contact to contact list
                    contactList.add(pinakas);
            } 
            }   catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
}

LogCat at tag&#34; mega&#34;所示:

08-11 19:14:37.527: D/mega(31807): {"polis":[{"startPoli1":"Athens","finalPoli1":"Tokio"},
{...........},...{"startPoli1":"","finalPoli1":""}]}

我认为这意味着&#34;一切都在掌控之中&#34; ..

LogCat现在显示:

08-11 20:00:11.995: W/System.err(4170): org.json.JSONException: No value for finalpoli1
08-11 20:00:11.995: W/System.err(4170):     at org.json.JSONObject.get(JSONObject.java:354)
08-11 20:00:11.995: W/System.err(4170):     at org.json.JSONObject.getString(JSONObject.java:510)
08-11 20:00:11.995: W/System.err(4170):     at com.example.tranfer.DataInsert$GetData.onPostExecute(DataInsert.java:79)

任何想法;

2 个答案:

答案 0 :(得分:0)

jObj是一个空的JSON对象:

 JSONObject jObj = new JSONObject(); // this is creating an empty json object

你应该只使用你输入的JSONObject json并从中获取数据。

答案 1 :(得分:0)

参数是finalPoli1区分大小写