使用 Protobuf(协议缓冲区)通过 Retrofit 进行 GET 和 POST 服务调用 [英] Make a GET and POST service call with Retrofit with the use of Protobuf (Protocol Buffer)
问题描述
任何人都可以给我一些例子,我们如何在改造中使用 protobuf - 我试过了,但由于一些错误而失败,让我给你一个我在这方面的实现示例.
Can anyone please give me some example how we can use protobuf in retrofit - I tried but its failed with some error , let me give you a sample of my implementation on that.
希望大家帮帮我.
ApiInterface.java
public interface ApiInterface {
@GET
Call<CommonProto.Country> makeGetRequest(@Url String url);
}
ApiClient.java
ApiClient.java
public class ApiClient {
public static final String BASE_URL = "**************************";
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(Proto3ConverterFactory.create())
.build();
}
return retrofit;
}
}
MainActivity.java
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<CommonProto.Country> call = apiService.makeGetRequest("Services/CountryServices/GetAllCountry");
call.enqueue(new Callback<CommonProto.Country>() {
@Override
public void onResponse(Call<CommonProto.Country> call, Response<CommonProto.Country> response) {
String bodyString = null;
try {
Log.e("RETROFIT ::::::: ", String.valueOf(response.body())+"TEST");
} catch (Exception e) {
Log.e("RETROFIT ERROR ::::::: ", e.getMessage()+"TEST");
e.printStackTrace();
}
}
@Override
public void onFailure(Call<CommonProto.Country> call, Throwable t) {
// Log error here since request failed
Log.e(TAG, t.toString());
}
}
);
当我以这种方式运行时,出现错误
when i run this way i got the error
java.lang.RuntimeException: com.google.protobuf.InvalidProtocolBufferException: Protocol message tag had invalid wire type.
我的 Proto.java 文件和 Proto.proto 文件都在此链接中,https://drive.google.com/folderview?id=0B4loQuzINvHCRUlNbk5LUXE1NXM&usp=sharing
my Proto.java file and also have Proto.proto file both are here in this link, https://drive.google.com/folderview?id=0B4loQuzINvHCRUlNbk5LUXE1NXM&usp=sharing
请让我知道如何执行此 GET Req 操作,同时我也在为 POST Req 苦苦挣扎.
Please let me know how to do this GET Req and also I was Struggling with POST Req.
推荐答案
你可以像这样创建界面
public interface LoginInterface {
@FormUrlEncoded
@POST("url goes here")
Call<LoginResponseData> getUserLoginDeatail(@FieldMap Map<String, String> fields);
}
制作一个retro文件的实例并调用类似这样的接口方法
make an instance of retro file and call interface method something like this
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("base url")
.build();
webApis = retrofit.create(WebApis.class);
Call<LoginResponseData> call = webApis.getCurrentRide(keyValue);
call.enqueue(new Callback<LoginResponseData>() {
@Override
public void onResponse(Call<LoginResponseData> call, Response<LoginResponseData> response) {
try {
} catch (Exception e) {
// customizedToast.showToast(context.getResources().getString(
// R.string.exception));
e.printStackTrace();
}
}
@Override
public void onFailure(Call<LoginResponseData> call, Throwable t) {
}
});
对于协议缓冲区,您可以在此处
for protocol buffer you can find a reference here
这篇关于使用 Protobuf(协议缓冲区)通过 Retrofit 进行 GET 和 POST 服务调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!