我正在使用retrofit 2.0,我有使用token和userId的发布请求,并且作为回应我得到一个包含arraylist的jsonobject
响应结构如下
{
"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;
}
任何帮助都将非常感谢...我无法得到答复...... 我以与上面给出的相同的方式回应...
请帮助
答案 0 :(得分: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();