从AsyncTask返回4个列表

时间:2013-09-10 11:30:46

标签: java android android-asynctask

我想从AsyncTask返回4个不同的列表作为结果,我已经使用Pair类来测试只返回两个,我想也许在Pair类中有问题

但是它给出了错误:

   FATAL EXCEPTION: main
   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hesham.sams/com.hesham.sams.ListActivity1}: java.lang.IllegalStateException: This is not a JSON Array.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
    at android.app.ActivityThread.access$700(ActivityThread.java:140)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4921)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
    at dalvik.system.NativeStart.main(Native Method)
   Caused by: java.lang.IllegalStateException: This is not a JSON Array.
    at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
    at com.hesham.sams.ListActivity1.getNames(ListActivity1.java:130)
    at com.hesham.sams.ListActivity1.onCreate(ListActivity1.java:48)
    at android.app.Activity.performCreate(Activity.java:5206)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
    ... 11 more

我的配对课程:

 public class Pair {

public List<Student> StudentNamesList;
public List<Student> StudentIdList;

public void setids(List<Student> StudentNamesList) {
    this.StudentNamesList = StudentNamesList;
}

public void setnames(List<Student> StudentIdList) {
    this.StudentIdList = StudentIdList;
}

public List<Student> getids() {
    return this.StudentNamesList;
}

public List<Student> getnames() {
    return this.StudentIdList;
}
}

我的AsyncTask类:

public class GetListAsync extends AsyncTask<Void, Void, Pair> {

private Activity activity;
private  ProgressDialog progressDialog;
Context context;

public GetListAsync(Activity activity, ProgressDialog progressDialog, 
                                                    Context context) {
    super();
    this.progressDialog = progressDialog;
    this.activity = activity;
    this.context = context;
}


    ArrayList<Student> StudentIdList = new ArrayList<Student>();
    ArrayList<Student> StudentNamesList = new ArrayList<Student>();
    Pair p = new Pair();

@Override
protected Pair doInBackground(Void... voids) {

// here getting some strings from database it's ok ,
// then assigne it to my studentidlist
    StudentIdList.add(get(String.valueOf(2)));
        p.setids(StudentNamesList);
    } catch (Exception e) { 
    }   

//Here is Second list 
 StudentIdList.add(get(String.valueOf(2)));

// here getting some strings from database it's ok ,
// then     assigne it to my studentidlist
p.setids(StudentIdList);
        } catch (Exception e) {
        }

 return p ;
}

    @Override
    protected void onPostExecute(Pair p) {
        GsonBuilder gsonb = new GsonBuilder();
        Gson gson = gsonb.create();
        String json = gson.toJson(p.getnames());

仅一种方法的ListActivity方法:

    public List<Student> getNames() {

    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

    String result = prefs.getString("studentnames", null);

    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonArray Jarray = parser.parse(result).getAsJsonArray();

    ArrayList<Student> lcs = new ArrayList<Student>();

    for (JsonElement obj : Jarray) {
        Student cse = gson.fromJson(obj, Student.class);
        lcs.add(cse);
    }

    Toast.makeText(getApplicationContext(), lcs.toString(),
            Toast.LENGTH_LONG).show();

    return (List<Student>) lcs;

} // end of on create

更新

这是我在AsyncTask onPostExecute中的JSON集

GsonBuilder gsonb = new GsonBuilder();
    Gson gson = gsonb.create();
    String json = gson.toJson(p.getids());

这里是我的JSON Get in Listview Activity类:

    public List<Student> getNames() {

    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());

    String result = prefs.getString("studentnames", null);

    Gson gson = new Gson();
    JsonParser parser = new JsonParser();
    JsonArray Jarray = parser.parse(result).getAsJsonArray();

    ArrayList<Student> lcs = new ArrayList<Student>();

    for (JsonElement obj : Jarray) {
        Student cse = gson.fromJson(obj, Student.class);
        lcs.add(cse);
    }

    Toast.makeText(getApplicationContext(), lcs.toString(),
            Toast.LENGTH_LONG).show();

    return (List<Student>) lcs;

注意:使用它时它可以工作,但我想使用我的所有4个列表:

String json = gson.toJson(StudentIdList); 

2 个答案:

答案 0 :(得分:0)

我引用你的帖子:

Caused by: java.lang.IllegalStateException: This is not a JSON Array.
at com.google.gson.JsonElement.getAsJsonArray(JsonElement.java:106)
at com.hesham.sams.ListActivity1.getNames(ListActivity1.java:130)
at com.hesham.sams.ListActivity1.onCreate(ListActivity1.java:48)

查看该代码。最有可能的是,你回错了。

答案 1 :(得分:0)

您正在解析的JSON不是JSON数组

JsonArray Jarray = parser.parse(result).getAsJsonArray();

看到JSON,以便调整到正确的对象图,但尝试使用JSON对象会很有帮助。

编辑:

  

“当我使用String json = gson.toJson(StudentIdList)时,它可以正常工作;”

不适用于:

String json = gson.toJson(p.getids());

因为getids()未返回StudentIdList

public List<Student> getids() {
    return this.StudentNamesList;
}

public List<Student> getnames() {
    return this.StudentIdList;
}

所以解决它:

public List<Student> getids() {
    return this.StudentIdList;
}

public List<Student> getnames() {
    return this.StudentNamesList;
}