2017-05-28 330 views
-5

你能幫我解決這個錯誤嗎?哪裏有問題?java.lang.IllegalStateException:期望BEGIN_ARRAY,但是BEGIN_OBJECT

private final String TAG = "MulaiUjian"; 
private RecyclerView recyclerView; 
private LinearLayoutManager layoutManager; 
private RecycleViewAdapter adapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_mulai_ujian); 
    recyclerView = (RecyclerView)findViewById(R.id.recycle_txtSoal); 
    layoutManager = new LinearLayoutManager(MulaiUjian.this); 
    recyclerView.setLayoutManager(layoutManager); 
    requestJsonObject(); 

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    if(getSupportActionBar()!=null){ 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setDisplayShowHomeEnabled(true); 
    } 
} 
private void requestJsonObject(){ 
    RequestQueue queue = Volley.newRequestQueue(this); 
    String url ="https://www.rootorial.com/worthit2/resgister/showalluser.php"; 
    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      Log.d(TAG, "Response " + response); 
      GsonBuilder builder = new GsonBuilder(); 
      Gson mGson = builder.create(); 
      List<ItemObject> posts = new ArrayList<ItemObject>(); 
      posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class)); 
      adapter = new RecycleViewAdapter(MulaiUjian.this, posts); 
      recyclerView.setAdapter(adapter); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      Log.d(TAG, "Error " + error.getMessage()); 
     } 
    }); 
    queue.add(stringRequest); 
} 

堆棧跟蹤:

過程:com.example.root.pmb,PID:7806 ** com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:預期BEGIN_ARRAY但 BEGIN_OBJECT在第1行第2列路徑$ ** at com.google.gson.Gson.fromJson(Gson.java:826) at com.google.gson.Gson.fromJson(Gson.java:779) at com。 google.gson.Gson.fromJson(Gson.java:728) com.exogle.goot.pmb.MulaiUjian $ 1.onResponse(MulaiUjian.java:59) at com.example.root.pmb.MulaiUjian $ 1 .onResponse(MulaiUjian.java:52) 在 com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 在 com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30 ) 在 com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 在android.os.Handler.handleCallback(Handler.java:739) 在android.os.Handler.dispatchMessage(韓droid.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5268) at java.lang.reflect.Method.invoke(本地方法) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:902) at com.android。 internal.os.ZygoteInit.main(ZygoteInit.java:697) 引起:java.lang.IllegalStateException:期望BEGIN_ARRAY,但 是BEGIN_OBJECT在第1行第2列路徑$ 在com.google.gson.stream.JsonReader.beginArray (JsonReader的.java:350) 在 com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:70) 在com.google.gson.Gson.fromJson(Gson.java:814) 在融爲一體。 google.gson.Gson.fromJson(Gson.java:779) at com.google.gson.Gson.fromJson(Gson.java:728) at com.google.gson.Gson.fromJson(Gson.java:700) 在com.example.root.pmb.MulaiUjian $ 1.onResponse(MulaiUjian.java:59) 在com.example.root.pmb.MulaiUjian $ 1.onResponse(MulaiUjian.java:52) 在 com.android.volley .toolbox.StringRequest.deliverResponse(StringRequest.java:60) 在 com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 在 com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 在android.os.Handler。在android.app.ActivityThread.main處使用handleCallback(Handler.java:739) ActivityThread。java:5268) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:902) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)

回答

0

你可能需要檢查你的模型或您收到的響應。您正在使用ItemObject[].class),但可能期望一個對象,因此反序列化失敗。

0

問題是,如果仔細查看,您的整個JSON響應是JSONObject。

{ 
    "id_soal": [{ 
     "id_soal": "So0045", 
     "isi_soal": "Pemilihan Umum tahun 1999 diikuti oleh:", 
     "bobot_nilai": "1", 
     "jawab_a": "10 partai politik", 
     "jawab_b": "18 partai politik", 
     "jawab_c": "45 partai politik", 
     "jawab_d": "48 partai politik", 
     "jawaban_benar": "D" 
    }, { 
     "id_soal": "So0039", 
     "isi_soal": "Prinsup Pembangunan Berkelanjutan adalah :", 
     "bobot_nilai": "1", 
     "jawab_a": "Pembangunan terus menerus", 
     "jawab_b": "Pembangunan yang tidak merusak lingkungan", 
     "jawab_c": "Pembangunan yang mempertimbangkan kehidupan generasiyang akan datang", 
     "jawab_d": "Pembangunan yang sesuai rencana pemerintah", 
     "jawaban_benar": "C" 
    }, { 
     "id_soal": "So0065", 
     "isi_soal": "Dimanakah tempat dibacakan naskah Proklamasi Indonesia ?", 
     "bobot_nilai": "1", 
     "jawab_a": "Jl. Diponogoro No. 56", 
     "jawab_b": "Jl. Pegangsaan timur No. 56", 
     "jawab_c": "Jl. Teuku Umar No. 67", 
     "jawab_d": "Jl. Cokroaminoto No. 67", 
     "jawaban_benar": "B" 
    } 
} 

和GSON解析您的回覆已成功需要需要有兩個模型類像下面

public class ItemObject { 

    private List<IdSoal> id_soal = null; 
    private Integer success; 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    public List<IdSoal> getIdSoal() { 
     return id_soal; 
    } 

    public void setIdSoal(List<IdSoal> idSoal) { 
     this.id_soal = idSoal; 
    } 

    public Integer getSuccess() { 
     return success; 
    } 

    public void setSuccess(Integer success) { 
     this.success = success; 
    } 

} 

,並

public class IdSoal { 

    private String id_soal; 
    private String isi_soal; 
    private String bobot_nilai; 
    private String jawab_a; 
    private String jawab_b; 
    private String jawab_c; 
    private String jawab_d; 
    private String jawaban_benar; 
    private Map<String, Object> additionalProperties = new HashMap<String, Object>(); 

    public String getId_soal() { 
     return id_soal; 
    } 

    public void setId_soal(String id_soal) { 
     this.id_soal = id_soal; 
    } 

    public String getIsi_soal() { 
     return isi_soal; 
    } 

    public void setIsi_soal(String isi_soal) { 
     this.isi_soal = isi_soal; 
    } 

    public String getBobot_nilai() { 
     return bobot_nilai; 
    } 

    public void setBobot_nilai(String bobot_nilai) { 
     this.bobot_nilai = bobot_nilai; 
    } 

    public String getJawab_a() { 
     return jawab_a; 
    } 

    public void setJawab_a(String jawab_a) { 
     this.jawab_a = jawab_a; 
    } 

    public String getJawab_b() { 
     return jawab_b; 
    } 

    public void setJawab_b(String jawab_b) { 
     this.jawab_b = jawab_b; 
    } 

    public String getJawab_c() { 
     return jawab_c; 
    } 

    public void setJawab_c(String jawab_c) { 
     this.jawab_c = jawab_c; 
    } 

    public String getJawab_d() { 
     return jawab_d; 
    } 

    public void setJawab_d(String jawab_d) { 
     this.jawab_d = jawab_d; 
    } 

    public String getJawaban_benar() { 
     return jawaban_benar; 
    } 

    public void setJawaban_benar(String jawaban_benar) { 
     this.jawaban_benar = jawaban_benar; 
    } 
} 

那麼所有你需要做的是像這

ItemObject itemObject = mGson.fromJson(response, ItemObject.class)); 
相關問題