使用Retrofit Android消耗WCF,响应主体为空,响应代码为405

时间:2017-01-07 12:59:14

标签: android json wcf retrofit

我有包含此方法的WCF服务:

[OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Bare,
        UriTemplate = "GetListInvititionFiltered?StartPage={StartPage}&userID={userID}&subject={subject}&customerName={customerName}&country={country}&invitationType={invitationType}&productGroup={productGroup}&companyGroup={companyGroup}&marketingEngineer={marketingEngineer}&proposalEngineer={proposalEngineer}&fromDate={fromDate}&toDate={toDate}&closingDate={closingDate}&sDataSourse={sDataSourse}&sCatalog={sCatalog}")]
    string GetListInvititionFiltered(string StartPage, string userID, string subject, string customerName, string country, string invitationType,
        string productGroup, string companyGroup, string marketingEngineer, string proposalEngineer, string fromDate, string toDate, string closingDate, string sDataSourse, string sCatalog);

当我从浏览器调用此方法时:

localhost:8080/AndroidWCFRestfull/AAPRestFullHttp.svc/GetListInvititionFiltered?StartPage=3&userID=104&subject=&customerName=&country=&invitationType=&productGroup=&companyGroup=&marketingEngineer=&proposalEngineer=&fromDate=01%2f02%2f2017&toDate=01%2f02%2f2017&closingDate=&sDataSourse=DB01-SRV&sCatalog=AAP

它在浏览器中返回此JSON:

"{\"invitationContainer\":{\"PagesCount\":\"3\",\"ListInvititionEntity\":[{\"Co_name\":\"UMM AL QURA CEMENT COMPANY\",\"In_invitation_id\":\"118680\",\"Has_document\":0,\"In_date\":\"1\/2\/2017 9:41:00 AM\",\"ClientName\":\"UMM AL QURA CEMENT COMPANY\",\"Type\":\"E\",\"ProductGroup\":\"LAB AND EDUCATION\",\"Subject\":\"Handheld Precise Colorimeter\",\"Stage\":\"NOT REGISTERED\",\"Countvalue\":0,\"BranchName\":\"Saudi Arabia\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"\",\"DocumentName\":\"\",\"DocumentID\":0},{\"Co_name\":\"GULF CEMENT COMPANY\",\"In_invitation_id\":\"119690\",\"Has_document\":1,\"In_date\":\"1\/2\/2017 2:36:00 PM\",\"ClientName\":\"GULF CEMENT COMPANY\",\"Type\":\"E\",\"ProductGroup\":\"INSTRUMENTATION\",\"Subject\":\"HERMO COUPLE FTBIDT\",\"Stage\":\"NWO\",\"Countvalue\":0,\"BranchName\":\"UAE\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"Invitation Documents\\\\UAE17-8-99-7-200D525136424CB4B8AD0EC6FB6E96FD.msg\",\"DocumentName\":\"NEW ENQUIRY---1000015936---THERMO COUPLE---SE16-739ALG05\",\"DocumentID\":75411},{\"Co_name\":\"KING HUSSEIN CANCER CENTER\",\"In_invitation_id\":\"118674\",\"Has_document\":1,\"In_date\":\"1\/2\/2017 9:20:00 AM\",\"ClientName\":\"KING HUSSEIN CANCER CENTER\",\"Type\":\"T\",\"ProductGroup\":\"LAB AND EDUCATION\",\"Subject\":\"lab miscellaneous supplies\",\"Stage\":\"NOT REGISTERED\",\"Countvalue\":0,\"BranchName\":\"JORDAN\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"Invitation Documents\\\\JOT17-1-99-1-8DFFF7EF2771462B9E0A2F8516ED155E.pdf\",\"DocumentName\":\"14525\",\"DocumentID\":74400},{\"Co_name\":\"CITY CEMENT COMPANY\",\"In_invitation_id\":\"119687\",\"Has_document\":1,\"In_date\":\"1\/2\/2017 2:28:00 PM\",\"ClientName\":\"CITY CEMENT COMPANY\",\"Type\":\"E\",\"ProductGroup\":\"MATERIAL\",\"Subject\":\"Hardox Plate\",\"Stage\":\"NWO\",\"Countvalue\":0,\"BranchName\":\"Saudi Arabia\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"Invitation Documents\\\\SAE17-5-99-5-B7CBAF310C0C4113806C0E139EAE9D9A.msg\",\"DocumentName\":\"FW  16 00896\",\"DocumentID\":75408},{\"Co_name\":\"TECTON LIMITED\",\"In_invitation_id\":\"118679\",\"Has_document\":1,\"In_date\":\"1\/2\/2017 9:38:00 AM\",\"ClientName\":\"TECTON LIMITED\",\"Type\":\"E\",\"ProductGroup\":\"FLOW\",\"Subject\":\"Upgrading of Mina Pumping Station at Silaa - Pumps\",\"Stage\":\"NOT REGISTERED\",\"Countvalue\":0,\"BranchName\":\"UAE\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"Invitation Documents\\\\UAE17-1-99-1-73A1537833314E849804306E9542E422.msg\",\"DocumentName\":\"NEW ENQUIRY    Our Ref   SP17-001SVT99 (TECTON ENGINEERING \\u0026 CONSTRUCTION LLC SUPPLY OF PUMPS  )    \",\"DocumentID\":74405},{\"Co_name\":\"JORDAN BROMINE COMPANY\",\"In_invitation_id\":\"118676\",\"Has_document\":1,\"In_date\":\"1\/2\/2017 9:27:00 AM\",\"ClientName\":\"JORDAN BROMINE COMPANY\",\"Type\":\"E\",\"ProductGroup\":\"POWER TRANSMISSION EQUIPMENT\",\"Subject\":\"SUPPLY OF MOTOR \\u0026 GEAR BOX\",\"Stage\":\"NOT REGISTERED\",\"Countvalue\":0,\"BranchName\":\"JORDAN\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"Invitation Documents\\\\JOE17-1-99-1-E7B5369AA747433AB0900E4748E94289.msg\",\"DocumentName\":\"FW  Rotary feeder inquiry\",\"DocumentID\":74398},{\"Co_name\":\"SALINE WATER CONVERSION CORPORATION\",\"In_invitation_id\":\"119678\",\"Has_document\":1,\"In_date\":\"1\/2\/2017 11:33:00 AM\",\"ClientName\":\"SALINE WATER CONVERSION CORPORATION\",\"Type\":\"E\",\"ProductGroup\":\"MATERIAL\",\"Subject\":\"SUPPLY OF WIRE\",\"Stage\":\"NWO\",\"Countvalue\":0,\"BranchName\":\"Saudi Arabia\",\"IsSeen\":0,\"DocumentPath_Mobile\":\"Invitation Documents\\\\SAE17-4-99-4-388BF4357F254111B80F8F3D43E4560A.msg\",\"DocumentName\":\"FW  RFQ # 1831992\",\"DocumentID\":75400}]}}"

我正在尝试使用Retrofit从Android应用程序中读取此JSON,这是服务方法的接口:

public interface AAPRestfulEndPointInterface {

@POST("GetListInvititionFiltered")
Call<InvitationsContainer> GetListInvititionFiltered(@Query("StartPage") String StartPage,
                                                     @Query("userID") String userID,
                                                     @Query("subject") String subject,
                                                     @Query("customerName") String customerName,
                                                     @Query("country") String country,
                                                     @Query("invitationType") String invitationType,
                                                     @Query("productGroup") String productGroup,
                                                     @Query("companyGroup") String companyGroup,
                                                     @Query("marketingEngineer") String marketingEngineer,
                                                     @Query("proposalEngineer") String proposalEngineer,
                                                     @Query("fromDate") String fromDate,
                                                     @Query("toDate") String toDate,
                                                     @Query("closingDate") String closingDate,
                                                     @Query("sDataSourse") String sDataSourse,
                                                     @Query("sCatalog") String sCatalog);

以下是该服务的代码调用:

Gson gson = new GsonBuilder()
            .setDateFormat("MM-dd-yyyy")
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    AAPRestfulEndPointInterface service = retrofit.create(AAPRestfulEndPointInterface.class);


    final Call<InvitationsContainer> apiCall = service.GetListInvititionFiltered("1", "104", "", "", "", "", "", "", "", "", "01/01/2017","01/02/2017", "", "DB01-SRV", "AAP");

    String url = apiCall.request().url().toString();
    Log.i("RETROFIT", url);

    apiCall.enqueue(new Callback<InvitationsContainer>() {
        @Override
        public void onResponse(Call<InvitationsContainer> call, Response<InvitationsContainer> response) {

            Log.i("RETROFIT", "onResponse Called");
            InvitationsContainer result = response.body();

        }

        @Override
        public void onFailure(Call<InvitationsContainer> call, Throwable t) {

            Toast.makeText(MainActivity.this, "Service Call Failure \n" + t.getMessage(), Toast.LENGTH_LONG).show();
            Log.e("RETROFIT", t.getMessage());

        }
    });

响应对象以null body和错误代码405返回的问题,任何人都可以帮我解决这个问题吗?

这是rawResponse:

  

响应{protocol = http / 1.1,代码= 405,消息=方法不允许,网址= http://xx.xx.xx.xx:8181/AndroidWCFRestfull/AAPRestFullHttp.svc/GetListInvititionFiltered?StartPage=1&userID=104&subject=&customerName=&country=&invitationType=&productGroup=&companyGroup=&marketingEngineer=&proposalEngineer=&fromDate=01/01/2017&toDate=01/02/2017&closingDate=&sDataSourse=DB01-SRV&sCatalog=AAP}

1 个答案:

答案 0 :(得分:1)

您的WCF服务仅接受GET请求。但是GetListInvititionFiltered中的AAPRestfulEndPointInterface方法被声明为POST

@POST("GetListInvititionFiltered")
Call<InvitationsContainer> GetListInvititionFiltered(..);

将其更改为GET以删除Method Not Allowed错误。

@GET("GetListInvititionFiltered")
Call<InvitationsContainer> GetListInvititionFiltered(..);