1

我想从 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());

在这里,我们在 Listview Activity 类中获取我的 JSON:

    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); 
4

2 回答 2

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;
}
于 2013-09-10T11:41:00.923 回答
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)

查看该代码。最有可能的是,您返回了错误的内容。

于 2013-09-10T11:38:52.777 回答