Android:Retrofit + RxJava向本地服务器发送POST请求。我收到了 - retrofit.HttpException:HTTP 404 Not Found。 [英] Android: Retrofit+RxJava send POST request to local server. I've receive - retrofit.HttpException: HTTP 404 Not Found.

查看:3410
本文介绍了Android:Retrofit + RxJava向本地服务器发送POST请求。我收到了 - retrofit.HttpException:HTTP 404 Not Found。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我参与了Android项目(Android Studio 1.4)。
我想连接到我的Apache服务器(不是REST API)来接收他的数据。重要提示:我已经对SWIFT iOS项目的服务器进行了相同的查询,我成功获得了POST / GET请求,因此服务器已经设置好并且运行良好。我想为Android做同样的事情。
Bellow code:

I worked on Android project (Android Studio 1.4). I want to connect to my Apache server (not REST API) to receive data from him. important NOTICE: I've made the same queries to my server for SWIFT iOS project, I was successfully get POST/GET request, so server is set up and working well. I want to do the same for Android. Bellow code:

1)我的Service.proto文件。我在我的客户端和服务器之间使用过protobuf。

1) My Service.proto file. I've used protobuf between my client and server.

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:wire-runtime: 1.8.0',
我有Java类 - BaseModelRequestProtobufDTO BaseModelResponseProtobufDTO

2) Using the library 'com.squareup.wire:wire-runtime:1.8.0', I've got Java classes - BaseModelRequestProtobufDTO and BaseModelResponseProtobufDTO

3)我的改造界面:

public interface RetrofitService {

   public interface RetrofitService 
{
    @Headers({"Cookie: bank=*************", "Content-Type: **************"})
    @POST("/baseMethod")
    Observable{BaseModelResponseProtobufDTO} baseRequest(@Body BaseModelRequestProtobufDTO baseModelRequestProtobufDTO);
}

4)我的功能登录登录和密码,发送到服务器。我想收到他的回复。

4) my function login take login and password, send to server. I want to receive response from him.

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对象发送到服务器,它包含两个字符串字段名称模型名称是简单的字符串名称。 model 是从json对象获得的字符串,而json对象又是从字典中发出的。字典是登录名和密码。

I send protobuf object to server, It's contain two string field name and model. name is simple string name. model is string which was obtained from json object which in turn was emmited from dictionary. Dictionary is login and password.

5)我的gradle:

5) My 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。

我做错了什么?请!

推荐答案

在BaseUrl
.baseUrl(http://77.108.126.41 :8080 / AAA / endpointshttp / protobuf),你需要以'/'
.baseUrl结尾( http://77.108.126.41:8080/AAA/endpointshttp/protobuf/

In BaseUrl .baseUrl("http://77.108.126.41:8080/AAA/endpointshttp/protobuf"), you need to end with '/' .baseUrl("http://77.108.126.41:8080/AAA/endpointshttp/protobuf/")

这篇关于Android:Retrofit + RxJava向本地服务器发送POST请求。我收到了 - retrofit.HttpException:HTTP 404 Not Found。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆