1

結果として AsyncTask から 4 つの異なるリストを返したいのですが、Pair クラスを使用して 2 つだけを返すようにテストしました。おそらく 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());

1 つのメソッドのみの 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 を使用します。

    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