我在Android项目(Android Studio 1.4)上工作。 我想连接到我的Apache服务器(而不是REST API)来接收他的数据。重要提示:我已经向SWIFT iOS项目的服务器进行了相同的查询,我成功地获得了POST/GET请求,因此服务器已建立并运行良好。我想为Android做同样的事情。 贝娄代码:Android:Retrofit + RxJava将POST请求发送到本地服务器。我收到 - retrofit.HttpException:未找到HTTP 404。
1)My Service.proto文件。我在我的客户端和服务器之间使用了protobuf。
option java_outer_classname = "Service";
message BaseModelRequestProtobufDTO
{
required string name = 1;
required string model = 2;
}
message BaseModelResponseProtobufDTO
{
required string response = 1;
}
2)使用图书馆com.squareup.wire:线运行:1.8.0“, 我有Java类 - BaseModelRequestProtobufDTO和BaseModelResponseProtobufDTO
3)我的更新接口:
public interface RetrofitService {
public interface RetrofitService
{
@Headers({"Cookie: bank=*************", "Content-Type: **************"})
@POST("/baseMethod")
Observable{BaseModelResponseProtobufDTO} baseRequest(@Body BaseModelRequestProtobufDTO baseModelRequestProtobufDTO);
}
4)我的函数登录取登录名和密码,发送给服务器。我想收到他的回复。
public static void login(String login, String password){
Log.v("json", "enter in Login = ");
// Prepare data as JSON object
Map<String,String> dictionary = new HashMap<>();
dictionary.put("password", password);
dictionary.put("login", login);
JSONObject JSONData = new JSONObject(dictionary);
String JSONModel = JSONData.toString();
Log.v("json", JSONModel);
UFCRequest loginRequest = new UFCRequest("login", JSONModel);
Log.v("json",loginRequest.baseModel.toString());
Log.v("json",loginRequest.getBaseModel().toString());
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://77.108.126.41:8080/AAA/endpointshttp/protobuf")
.addConverterFactory(WireConverterFactory.create())
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.build();
RetrofitService retrofitService = retrofit.create(RetrofitService.class);
retrofitService.baseRequest(loginRequest.getBaseModel())
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber{BaseModelResponseProtobufDTO}() {
@Override
public void onCompleted() {
Log.v("json", "onCompleted");
}
@Override
public void onError(Throwable e) {
Log.v("json", "onError " + e);
}
@Override
public void onNext(BaseModelResponseProtobufDTO baseModelResponseProtobufDTO) {
Log.v("json", baseModelResponseProtobufDTO.response.toString());
Log.v("json", "onNext");
}
});
}
我送protobuf的对象服务器,它包含两个字符串字段名和模式。 名称是简单的字符串名称。 模型是从json对象获得的字符串,而json对象又是从字典中提取的。 Dictionary是登录名和密码。
5)我gradle这个:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
compile 'com.squareup.wire:wire-runtime:1.8.0'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta1'
compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2'
compile 'com.squareup.retrofit:converter-wire:2.0.0-beta2'
compile 'io.reactivex:rxandroid:1.0.1'
}
6)我的结果: 我已经收到 - retrofit.HttpException:HTTP 404未找到。
我做错了什么?请!
这意味着您的网址是错 – Tauqir
我可以调用服务器通过改造,如果它不是REST API?简单的服务器 – Gilb007
@ Tauqir,你是对的。 Omg,翻新+ rxjava工程perfetc。 @POST( “http://77.108.126.41:8080/AAA/endpointshttp/protobuf/baseMethod”)。 Thnx很多 – Gilb007