致命异常:AsyncTask#1 java.lang.RuntimeException:执行doInBackground()时发生错误

时间:2015-04-28 08:37:14

标签: android json web-services

我想从Web服务获取json对象并在列表视图中显示它们但发生错误

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.activity_contacte);
    // Execute DownloadJSON AsyncTask
    new DownloadJSON().execute();
} 
 // create the class DownloadJSON
private class DownloadJSON extends AsyncTask<Integer, Integer, Integer> {


    @Override
    protected Integer doInBackground(Integer... requests) {

              // try and catch 

        try {
          // Create an array
            arraylist = new ArrayList<HashMap<String, String>>();
            // Retrieve JSON Objects from the given URL address
            jsonobject = JSONfunctions.getJSONfromURL("http://192.168.1.64:8080/attijarimobile.service-1/service/contacte");
            for (int i = 0; i < jsonarray.length(); i++) {
                HashMap<String, String> map = new HashMap<String, String>();
                jsonobject = jsonarray.getJSONObject(i);
              // Retrive JSON Objects
                map.put("nomagence", jsonobject.getString("nomagence"));
                map.put("adresse", jsonobject.getString("adresse"));
                map.put("tel", jsonobject.getString("tel"));
                map.put("mail", jsonobject.getString("mail"));
             // Set the JSON Objects into the array
                arraylist.add(map);
            }

        } // catch 
    catch (JSONException e) {
            Log.e("Error", e.getMessage());

            e.printStackTrace();
        }
        return 0;
    }    
} 

这是logcat

04-28 09:07:44.468    8648-9077/attijari.com.attijarimobileapp E/log_tag﹕     Error parsing data org.json.JSONException:                                                 Value   [{"id":1,"mail":"attijarimobile@gmail.com","adresse":" AGENCE KHEIREDDINE, Avenue Habib Bourguiba,La Goulette ,2060tunis","nomagence":"AGENCE KHEIREDDINE","tel":"(+216) 71 730 373\/71 730 374"},{"id":2,"mail":"attijarimobile@gmail.com","adresse":"53, AVENUE DE PARIS CP 1000 - Tunis","nomagence":"LE PASSAGE","tel":"(+216) 71 342278"},{"id":3,"mail":"attijarimobile@gmail.com","adresse":" AGENCE CHARGUIA AEROPORT; rue des des entrepreneurs Immeuble Hertz, Z.I charguia2 ,2035tunis","nomagence":"AGENCE CHARGUIA AEROPORT","tel":"(+216) 71 941 855\/71 941 844"},{"id":4,"mail":"attijarimobile@gmail.com","adresse":" AGENCE EL MANAR ; 16 avenue abdelaziz al saoud el manar2 ,2092 el manar2","nomagence":"AGENCE EL MANAR","tel":"(+216) 71 871 731\/71 888 024"},{"id":5,"mail":"attijarimobile@gmail.com","adresse":" IMMEUBLE AZIZ, RUE 8368 MONTPLAISIR CP 1073 - Tunis","nomagence":"MONTPLAISIR","tel":"(+216) 71 950 913\/71 950 414"},{"id":6,"mail":"attijarimobile@gmail.com","adresse":"RUE DU LAC ANNECY CP 1053 - Tunis","nomagence":"LES BERGES DU LAC","tel":"(+216) 71 964 654"},{"id":7,"mail":"attijarimobile@gmail.com","adresse":"RESIDENCE ESPLANADE AVENUE HABIB BOURGUIBA CP 2007 - Marsa Plage","nomagence":"LA MARSA","tel":"(+216) 71 729 481"},{"id":8,"mail":"attijarimobile@gmail.com","adresse":"ANGLE DE AVENUE FETHI ZOUHEIR ET RUE DE PALESTINE CP 2083 - Ariana","nomagence":"EL GHAZELA","tel":"(+216) 71 877 716"},{"id":9,"mail":"attijarimobile@gmail.com","adresse":"9, AVENUE HABIB BOURGUIBA CP 2080 - Ariana","nomagence":"ARIANA","tel":"(+216) 71 710 305"},{"id":10,"mail":"attijarimobile@gmail.com","adresse":"39 BIS AVENUE HEDI NOUIRA CP 2037 - Ariana","nomagence":"ENNASR II","tel":"(+216) 70 830 012"},{"id":11,"mail":"attijarimobile@gmail.com","adresse":" ANGLE DE L AVENUE DE L'UMA ET RUE EL MOEZ IBN BADIS CP 2036 - Ariana","nomagence":"SOUKRA","tel":"(+216) 70 696 150\/70 696 081"}] of type org.json.JSONArray cannot be converted to JSONObject
04-28 09:07:44.538    8648-9077/attijari.com.attijarimobileapp E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)
     Caused by: java.lang.NullPointerException
            at attijari.com.attijarimobileapp.Agences$DownloadJSON.doInBackground(Agences.java:89)
            at attijari.com.attijarimobileapp.Agences$DownloadJSON.doInBackground(Agences.java:61)
            at android.os.AsyncTask$2.call(AsyncTask.java:287)
            at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:856)

2 个答案:

答案 0 :(得分:1)

我无法看到jsonarray声明或初始化。所以你需要改变你的代码:

 try {
      // Create an array
        arraylist = new ArrayList<HashMap<String, String>>();
        // Retrieve JSON Objects from the given URL address
        jsonobject = JSONfunctions.getJSONfromURL("http://192.168.1.64:8080/attijarimobile.service-1/service/contacte");
        JSONArray jsonarray = jsonobject.getJSONArray("Value");
        for (int i = 0; i < jsonarray.length(); i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            jsonobject = jsonarray.getJSONObject(i);
          // Retrive JSON Objects
            map.put("nomagence", jsonobject.getString("nomagence"));
            map.put("adresse", jsonobject.getString("adresse"));
            map.put("tel", jsonobject.getString("tel"));
            map.put("mail", jsonobject.getString("mail"));
         // Set the JSON Objects into the array
            arraylist.add(map);
        }

    } // catch 
        catch (JSONException e) {
        Log.e("Error", e.getMessage());

        e.printStackTrace();
    }

如果你已经宣布jsonArray,那就写下这个

jsonarray = jsonobject.getJSONArray("Value");

答案 1 :(得分:0)

Caused by: java.lang.NullPointerException at attijari.com.attijarimobileapp.Agences$DownloadJSON.doInBackground(Agences.java:89)

检查此行Agences.java:89