2017-12-27 286 views
0

我得到了錯誤java.lang.illegalstateException:預期BEGIN_OBJECT但行1列42是BEGIN_ARRAY當與改造呼叫API,這裏是我的代碼:java.lang.illegalstateException:預期BEGIN_OBJECT但BEGIN_ARRAY改造

下面是我的界面:

@FormUrlEncoded 
@POST("myCollection") 
Call<APIResponse<List<MyCollection>>> getMyCollection(
     @Field("caller_id") String caller_id 
); 

這裏是召喚:

public class UploadVideoToneActivity extends BaseActivity{ 
List<MyCollection> collection = new ArrayList<>(); 

private void loadCollection() { 

    ContactItem callerID = SessionManager.getProfile(this); 
    Call<APIResponse<MyCollection>> call = ServicesFactory.getService().getMyCollection(callerID.caller_id); 
    call.enqueue(new Callback<APIResponse<MyCollection>>() { 
     @Override 
     public void onResponse(Call<APIResponse<MyCollection>> call, Response<APIResponse<MyCollection>> response) { 
      if (response.isSuccessful() && response.body().isSuccessful()) { 
       List<MyCollection> data = (List<MyCollection>) response.body().data; 
       if (data != null) { 
        collection.clear(); 
        collection.addAll(data); 
        rvCollection.getAdapter().notifyDataSetChanged(); 
       } 
      } 
      else { 
       Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call<APIResponse<MyCollection>> call, Throwable t) { 
      Toast.makeText(UploadVideoToneActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

這裏是JSON格式響應從API, 下面是API的格式JSON響應,

{ 
"code": 200, 
"error_message": null, 
"data": [ 
    [ 
     { 
      "caller_id": "44", 
      "content_id": "003", 
      "alias": "Eli Sugigi", 
      "judul": "Angkat sekarang juga", 
      "source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "sub_start": "2017-12-27 14:17:10", 
      "sub_end": "2018-01-03 00:00:00" 
     } 
    ], 
    [ 
     { 
      "caller_id": "44", 
      "content_id": "002", 
      "alias": "Eli Sugigi", 
      "judul": "Mas Ganteng Angkat Dong", 
      "source_content":"source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "thumb_pic":"source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "sub_start": "2017-12-27 15:52:40", 
      "sub_end": "2018-01-03 00:00:00" 
     } 
    ] 
    ] 
    } 

這裏是MyCollection的類:

public class MyCollection implements Parcelable { 
@SerializedName("content_id") 
@Expose 
public String content_id; 
@SerializedName("alias") 
@Expose 
public String alias ; 
@SerializedName("judul") 
@Expose 
public String judul; 
@SerializedName("source_content") 
@Expose 
public String source_content; 
@SerializedName("thumb_pic") 
@Expose 
public String thumb_pic; 
@SerializedName("sub_start") 
@Expose 
public String sub_start ; 
@SerializedName("sub_end") 
@Expose 
public String sub_end ; 



protected MyCollection(Parcel in) { 
    content_id = in.readString(); 
    alias = in.readString(); 
    judul = in.readString(); 
    source_content = in.readString(); 
    thumb_pic = in.readString(); 
    sub_start = in.readString(); 
    sub_end = in.readString(); 
} 

public static final Creator<MyCollection> CREATOR = new Creator<MyCollection>() { 
    @Override 
    public MyCollection createFromParcel(Parcel in) { 
     return new MyCollection(in); 
    } 

    @Override 
    public MyCollection[] newArray(int size) { 
     return new MyCollection[size]; 
    } 
}; 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel parcel, int i) { 
    parcel.writeString(content_id); 
    parcel.writeString(alias); 
    parcel.writeString(judul); 
    parcel.writeString(source_content); 
    parcel.writeString(thumb_pic); 
    parcel.writeString(sub_start); 
    parcel.writeString(sub_end); 
} 
} 
+0

顯示您的MyCollection的類 – ABDevelopers

+0

顯示您ApiResponse 類 – ABDevelopers

回答

1

您提供的所有JSON首先是不正確的:

這是正確的JSON:

{ 
    "code": 200, 
    "error_message": null, 
    "data": [{ 
      "caller_id": "44", 
      "content_id": "003", 
      "alias": "Eli Sugigi", 
      "judul": "Angkat sekarang juga", 
      "source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "sub_start": "2017-12-27 14:17:10", 
      "sub_end": "2018-01-03 00:00:00" 
     }, 
     { 
      "caller_id": "44", 
      "content_id": "002", 
      "alias": "Eli Sugigi", 
      "judul": "Mas Ganteng Angkat Dong", 
      "source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "sub_start": "2017-12-27 15:52:40", 
      "sub_end": "2018-01-03 00:00:00" 
     } 
    ] 
} 

後來你的迴應類用這個作爲響應等級:

public class MyResponseClass 
{ 
    private String error_message; 

    private ArrayList<Data> data; 

    private String code; 

    public String getError_message() 
    { 
     return error_message; 
    } 

    public void setError_message (String error_message) 
    { 
     this.error_message = error_message; 
    } 

    public ArrayList<Data> getData() 
    { 
     return data; 
    } 

    public void setData (ArrayList<Data> data) 
    { 
     this.data = data; 
    } 

    public String getCode() 
    { 
     return code; 
    } 

    public void setCode (String code) 
    { 
     this.code = code; 
    } 

    @Override 
    public String toString() 
    { 
     return "ClassPojo [error_message = "+error_message+", data = "+data+", code = "+code+"]"; 
    } 
} 

後來使用MyResponseData作爲您的改進數據解析器:

ContactItem callerID = SessionManager.getProfile(this); 
    Call<MyResponseData> call = ServicesFactory.getService().getMyCollection(callerID.caller_id); 

,並在您onResponse:

@Override 
     public void onResponse(Call<MyResponseData> call, Response<MyResponseData> response) { 
      if (response.isSuccessful() && response.body().isSuccessful()) { 
       //here collection will be arraylist of MyCollection class 
        collection.clear(); 
        collection.addAll(response.body().getData()); 
        rvCollection.getAdapter().notifyDataSetChanged(); 

      } 
      else { 
       Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show(); 
      } 
     } 

測試它,如果需要的話

+0

謝謝ABDevelopers, so「data」:[ [ [ ] [ double array is can not be applied? – asqa

+0

不,它是invalida json首先 – ABDevelopers

+0

在數組中你可以有多個對象[{},{}],如果你想嵌套的數組不是[{[{}]},{[{}]}] – ABDevelopers

1

問題是你所得到的「JSONArray的迴應和你想收到在JSONObject

您應該使用'List'作爲返回類型getMyCollection(callerID.caller_id)方法。然後執行該方法。

onResponse方法,你必須得到像下面

List<MyCollection> data = (List<MyCollection>) response.body() 

更新

您可以點擊這裏查看列表(http://pojo.sodhanalibrary.com/

把你JSON響應,然後按提交。您將獲得Pojo類結構。

+0

感謝Asish,我媒體鏈接更改我的代碼在onResponse,但仍然得到了同樣的錯誤.. :( 你有另一種解決方案? – asqa

+0

的「JSON」響應你 –

+0

請檢查Json上面,我已經更新了新的.. :) – asqa

1

作出輕微修改嘗試從APIResponse<MyCollection>更改爲APIResponse<List<MyCollection>>

因此,代碼變爲:

public class UploadVideoToneActivity extends BaseActivity{ 
List<MyCollection> collection = new ArrayList<>(); 

private void loadCollection() { 

    ContactItem callerID = SessionManager.getProfile(this); 
    Call<APIResponse<List<MyCollection>>> call = ServicesFactory.getService().getMyCollection(callerID.caller_id); 
    call.enqueue(new Callback<APIResponse<List<MyCollection>>>() { 
     @Override 
     public void onResponse(Call<APIResponse<List<MyCollection>>> call, Response<APIResponse<List<MyCollection>>> response) { 
      if (response.isSuccessful() && response.body().isSuccessful()) { 
       List<MyCollection> data = (List<MyCollection>) response.body().data; 
       if (data != null) { 
        collection.clear(); 
        collection.addAll(data); 
        rvCollection.getAdapter().notifyDataSetChanged(); 
       } 
      } 
      else { 
       Toast.makeText(UploadVideoToneActivity.this, response.errorBody().toString(), Toast.LENGTH_LONG).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call<APIResponse<List<MyCollection>>> call, Throwable t) { 
      Toast.makeText(UploadVideoToneActivity.this, t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); 
     } 
    }); 
} 

而且更新您的Json(部分)至:

"data": [{ 
      "caller_id": "44", 
      "content_id": "003", 
      "alias": "Eli Sugigi", 
      "judul": "Angkat sekarang juga", 
      "source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "sub_start": "2017-12-27 14:17:10", 
      "sub_end": "2018-01-03 00:00:00" 
     }, 
     { 
      "caller_id": "44", 
      "content_id": "002", 
      "alias": "Eli Sugigi", 
      "judul": "Mas Ganteng Angkat Dong", 
      "source_content": "http://bla.bla.mp4", 
      "thumb_pic": "http://bla.bla.mp4", 
      "sub_start": "2017-12-27 15:52:40", 
      "sub_end": "2018-01-03 00:00:00" 
     } 
    ] 

希望它有幫助!

+0

如何,如果我的JSON這樣 「數據」:[{ [ {現在我不能進去這裏的數據(在第2個數組)} ] } – asqa

+0

如果你把你的JSON,如:** [{ [{**那麼第二個數組被認爲完全不同於第一個數組,您可能必須爲每個'JsonArray'指定不同的鍵來獲取適當的數據;如**「data」:{「key1」:[{}],「key2」:[{}]} ** –

相關問題