2016-11-24 42 views
0

我使用改裝2.0和我有令牌和帳戶及響應我得到一個包含數組列表Android的改造2.0找問題

響應的結構的JSONObject的POST請求如下

{ 
"communities": [ 
    { 
    "commId": 1, 
    "name": "Enginnering" 
    }, 
    { 
    "commId": 2, 
    "name": "Student" 
    }, 
    { 
    "commId": 3, 
    "name": "Banking" 
    }, 
    { 
    "commId": 4, 
    "name": "Teacher" 
    }, 
    { 
    "commId": 5, 
    "name": "Government" 
    }, 
    { 
    "commId": 6, 
    "name": "Political" 
    } 
] 
} 

的模型類結構如下

CommunityModel implements Serializable{ 

    public static final String KEY_COMMUNITY_ID = "commId"; 
    public static final String KEY_COMMUNITY_NAME = "name"; 
    public static final String KEY_CREATED_AT="c_at"; 
    public static final String KEY_UPDATED_AT="u_at"; 
    public static final String KEY_AUTHORIZATION = "Authorization"; 
    public static final String KEY_COMMUNITIES="communities"; 

    @SerializedName(KEY_COMMUNITY_ID) 
    int communityid; 

    @SerializedName(KEY_COMMUNITY_NAME) 
    String communityName; 

    @SerializedName(KEY_CREATED_AT) 
    String createdAt; 

    @SerializedName(KEY_UPDATED_AT) 
    String updatedAt; 


    public CommunityModel(int communityid, String communityName, String createdAt, String updatedAt) 
    { 
     this.communityid=communityid; 
     this.communityName=communityName; 
     this.createdAt=createdAt; 
     this.updatedAt=updatedAt; 
    } 

    public int getCommunityid() { 
     return communityid; 
    } 

    public void setCommunityid(int communityid) { 
     this.communityid = communityid; 
    } 

    public String getCommunityName() { 
     return communityName; 
    } 

    public void setCommunityName(String communityName) { 
     this.communityName = communityName; 
    } 

    public String getCreatedAt() { 
     return createdAt; 
    } 

    public void setCreatedAt(String createdAt) { 
     this.createdAt = createdAt; 
    } 

    public String getUpdatedAt() { 
     return updatedAt; 
    } 

    public void setUpdatedAt(String updatedAt) { 
     this.updatedAt = updatedAt; 
    } 
} 

我的改型接口

@FormUrlEncoded 
    @POST("/v1/communities/") 
    Call<ArrayList<CommunityModel>> getCommunities(
      @Query(CommunityModel.KEY_AUTHORIZATION) String servertoken, 
      @Field(UserAccount.KEY_USER_ID) int type, 
      @Field(CommunityModel.KEY_COMMUNITIES) ArrayList<CommunityModel>communityList 
    ); 

我改造助手得到的ArrayList是如下

public ArrayList<CommunityModel> getcommunities() { 
     final ArrayList<CommunityModel> returnCommunityList = new ArrayList<>(); 
     //final int userId = mIntent.getIntExtra(UserAccount.KEY_USER_ID, 0); 
     final String serverToken ="Basic"+" "+localSession.getServerToken(); 
     DebugLog.i(NetworkOperationService.class.getSimpleName(), "servertoken"+serverToken); 
     Call<ArrayList<CommunityModel>> communityCall = mNetworkOperation.getCommunities(serverToken, localSession.getUserId(),); 
     communityCall.enqueue(new Callback<ArrayList<CommunityModel>>() { 
      @Override 
      public void onResponse(Call<ArrayList<CommunityModel>> call, Response<ArrayList<CommunityModel>> response) { 

       DebugLog.i(NetworkOperationService.class.getSimpleName(), "servertoken"+serverToken+"userid="+localSession.getUserId()); 

       if (response!=null && response.code()==200) 
       { 
        DebugLog.i(NetworkOperationService.class.getSimpleName(), "200"); 
        ArrayList<CommunityModel> arrayList = response.body(); 

        for (int i=0;i<arrayList.size();i++) 
        { 
         returnCommunityList.add(arrayList.get(i)); 
         DebugLog.i(NetworkOperationService.class.getSimpleName(), arrayList.get(i).getCommunityName()); 
        } 
       } 
       else{ 
        DebugLog.i(NetworkOperationService.class.getSimpleName(), "respose code"+response.code()); 
       } 
      } 
      @Override 
      public void onFailure(Call<ArrayList<CommunityModel>> call, Throwable t) { 
      } 
     }); 

     return returnCommunityList; 
    } 

任何幫助,將不勝感激......我不能夠獲得響應... i灣以同樣的方式迴應如上面給出...

請幫助

+0

任何錯誤?請添加日誌 –

+0

我收到錯誤400 ...因爲它接受頭認證....所以現在我已經添加頭給它的迴應...我想知道查詢()參數和頭() –

回答

0

你可以試試下面的代碼來發送報頭字段與改造。

public interface UserService { 
    @GET("/tasks") 
    Call<List<Task>> getTasks(@Header("Content-Range") String contentRange); 
} 

否則,您可以通過使用OkHttpClient嘗試:

OkHttpClient.Builder httpClient = new OkHttpClient.Builder(); 
httpClient.addInterceptor(new Interceptor() { 
    @Override 
    public Response intercept(Interceptor.Chain chain) throws IOException { 
     Request original = chain.request(); 

     Request request = original.newBuilder() 
      .header("User-Agent", "Your-App-Name") 
      .header("Accept", "application/vnd.yourapi.v1.full+json") 
      .method(original.method(), original.body()) 
      .build(); 

     return chain.proceed(request); 
    } 
} 

OkHttpClient client = httpClient.build(); 
Retrofit retrofit = new Retrofit.Builder() 
    .baseUrl(API_BASE_URL) 
    .addConverterFactory(GsonConverterFactory.create()) 
    .client(client) 
    .build();