Android: Retrofit+RxJava send POST request to loca

2019-06-10 22:44发布

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) 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) Using the library 'com.squareup.wire:wire-runtime:1.8.0', I've got Java classes - BaseModelRequestProtobufDTO and BaseModelResponseProtobufDTO

3) My Retrofit interface:

public interface RetrofitService {

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

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");
                }
            });

     }

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) 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) My result : I've receive - retrofit.HttpException: HTTP 404 Not Found.

What I'm doing wrong? Please!

1条回答
冷血范
2楼-- · 2019-06-10 23:19

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/")

查看更多
登录 后发表回答