【问题标题】:How to call asp.net webservice using retrofit?如何使用改造调用 asp.net webservice?
【发布时间】:2018-03-22 06:41:33
【问题描述】:

我在 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/

标签: android retrofit2 android-webservice


【解决方案1】:

您收到错误是因为您首先在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);
    }

【讨论】:

  • GET /Client.asmx/Client_Login?MobileNo=string&Pass=string&AppVer=string 我想这样通过
  • POST /Client.asmx/Client_Login
  • 如果不是对服务器的json请求,试试@UrlEncode。默认改造允许 json 请求。
猜你喜欢
  • 2012-08-18
  • 1970-01-01
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 2016-04-17
相关资源
最近更新 更多