2016-11-24 1 views
0

J'utilise retrofit 2.0 et j'ai demande de poste avec jeton et userId et en réponse je reçois un JsonObject contenant arraylistAndroid Retrofit 2.0 Obtenez problème

La structure de la réponse est comme ci-dessous

{ 
"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" 
    } 
] 
} 

Le structure de classe modèle est comme ci-dessous

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; 
    } 
} 

Mon interface rétrofit

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

Mon aide de retrofit pour obtenir ArrayList est comme ci-dessous

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; 
    } 

Toute aide serait appréciée ... Je ne suis pas en mesure d'obtenir la réponse ... i wan la réponse de la même manière comme indiqué ci-dessus ...

s'il vous plaît aider

+0

toute erreur? s'il vous plaît ajouter le journal –

+0

je reçois erreur 400 ... parce qu'il accepte les en-têtes pour l'authentification .... alors maintenant j'ai ajouté l'en-tête sa réponse donnant ... je voudrais savoir la différence entre le paramètre query() et les en-têtes() –

Répondre

0

Vous pouvez essayer le code ci-dessous pour envoyer champ d'en-tête avec mise à niveau.

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

Sinon, vous pouvez essayer en utilisant 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();