android - 如何使用改造调用 asp.net webservice?

标签 android retrofit2 android-webservice

我在 android 中调用了 asp.net webservice,但它给出了错误 baseUrl must end in/

这是我的网址

 private static String url = "http://192.138.0.100/Client.asmx?op=Client_Login";
  //create Interface
    public interface ApiInterface {
        @GET("api/{MobileNo}/{Pass}/{AppVer}")
        Call<Login> authenticate(@Path("MobileNo") String MobileNo, @Path("Pass") String password, @Path("AppVer") String AppVer);
        @POST("api/{MobileNo}/{Pass}/{AppVer}")
        Call<Login> registration(@Path("MobileNo") String email, @Path("Pass") String password, @Path("AppVer") String AppVer);
    }

这个方法用来调用webservice但是报错

 private void loginProcessWithRetrofit(final String mobilno, String pwd,String Appver){
    ApiInterface mApiService = this.getInterfaceService();
    Call<Login> mService = mApiService.authenticate(mobilno, pwd,Appver);
    mService.enqueue(new Callback<Login>() {
        @Override
        public void onResponse(Call<Login> call, Response<Login> response) {
            Login mLoginObject = response.body();
            String returnedResponse = mLoginObject.isLogin;
            Toast.makeText(LoginActivity.this, "Returned " + returnedResponse, Toast.LENGTH_LONG).show();
            //showProgress(false);
            if(returnedResponse.trim().equals("1")){
                // redirect to Main Activity page

            }
            if(returnedResponse.trim().equals("0")){
                // use the registration button to register
               // failedLoginMessage.setText(getResources().getString(R.string.registration_message));
               // mPasswordView.requestFocus();
            }
        }
        @Override
        public void onFailure(Call<Login> call, Throwable t) {
            call.cancel();
            Toast.makeText(LoginActivity.this, "Please check your network connection and internet permission", Toast.LENGTH_LONG).show();
        }
    });
}
private ApiInterface getInterfaceService() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(url)
            .addConverterFactory(SimpleXmlConverterFactory.create())
            .build();
    final ApiInterface mInterfaceService = retrofit.create(ApiInterface.class);
    return mInterfaceService;
}

最佳答案

您收到错误是因为您首先在 baseurl 中添加了查询参数。

您的网址:http://192.138.0.100/Client.asmx?op=Client_Login/api/{MobileNo}/{Pass}/{AppVer}

应该是这样的:http://192.138.0.100/Client_Login/api/{MobileNo}/{Pass}/{AppVer}

查询参数总是出现在 URL 的末尾

请检查一次您的网址。

正如您在评论中提到的,您可以通过以下方式做到这一点:

 public interface ApiInterface {
        @GET("api/")
        Call<Login> authenticate(@Query("MobileNo") String MobileNo, @Query("Pass") String password, @Query("AppVer") String AppVer);
        @POST("api/")
        Call<Login> registration(@Query("MobileNo") String MobileNo, @Query("Pass") String password, @Query("AppVer") String AppVer);
    }

关于android - 如何使用改造调用 asp.net webservice?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49422142/

相关文章:

android - Webservice,无法在程序中设置强制转换

android - 照片未出现在 RecyclerView 中

安卓VideoView : How do I validate an illegal character in my url?

android - 修改文件上传socket超时和异常

android - android中从右到左的布局

java - 如何使用动态键为 json 创建 POJO 类以进行改造?

android - OnActivityResult Activity 错误 : Failure delivering result ResultInfo

android - 如何将以下参数发送到 json webservice

安卓 : Empty SoapAction

java - Android,将数据推送到一组订阅线程的最佳方式?