以下代码调用了webservice,我希望在Android上使用Retrofit
实现,但是我收到此错误:
No Retrofit annotation found
使用Web Service
调用CURL
:
curl -H "X-Auth-Token: 9HqLlyZOugoStsXCUfD_0YdwnNnunAJF8V47U3QHXSq" \
-H "X-User-Id: aobEdbYhXfu5hkeqG" \
http://localhost:3000/api/v1/channels.list
我用上面的代码编写了这个界面:
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Header;
public interface RocketRestfulService {
@GET("/api/v1/channels.list")
Call<List<ChannelsList>> getChannelsList(
@Header("X-Auth-Token") String AuthToken,
@Header("X-User-Id") String UserId,
ChannelsList channelsList);
}
我正在通过此代码调用此休息Web Service
:
ChannelsList channelsList = new ChannelsList();
Call<List<ChannelsList>> call = rocketRestfulService.getChannelsList(
"HNv1VtMiyUky2RkXWUydyj4f2bfciQ6DzVQgKULSwfe",
"Wz9ex2N2z9zzJWdzD",
channelsList);
call.enqueue(new Callback<List<ChannelsList>>() {
@Override
public void onResponse(Call<List<ChannelsList>> call, final Response<List<ChannelsList>> response) {
Log.e("contentLength ", response.code() + "");
}
@Override
public void onFailure(Call<List<ChannelsList>> call, Throwable t) {
t.printStackTrace();
}
});
我的代码问题是什么,我无法调用,我收到错误?
答案 0 :(得分:0)
您忘记了@Body
注释。由于您必须发送正文,因此必须为您的API创建POST
。如果不是POST
,您必须找到如何发送ChannelsList
才能成为GET
请求,因为这取决于您的服务器实施。
@POST("/api/v1/channels.list")
Call<List<ChannelsList>> getChannelsList(
@Header("X-Auth-Token") String AuthToken,
@Header("X-User-Id") String UserId,
@Body ChannelsList channelsList);
答案 1 :(得分:0)
问题通过以下代码解决:
接口:
public interface RocketRestfulService {
@GET("/api/v1/channels.list")
Call<ResponseBody> getChannelsList(
@Header("X-Auth-Token") String AuthToken,
@Header("X-User-Id") String UserId);
}
致电请求并获得回复:
ChannelsList channelsList = new ChannelsList();
Call<ResponseBody> call = rocketRestfulService.getChannelsList(
"HNv1VtMiyUky2RkXWUydyj4f2bfciQ6DzVQgKULSwfe",
"Wz9ex2N2z9zzJWdzD");
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String jsonString = response.body().string();
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray jsonarray = jsonObject.getJSONArray("channels");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Log.e("Err: ", t.getMessage());
}
});