使用 Retrofit Android 使用 WCF,响应正文为空,响应代码 405
Posted
技术标签:
【中文标题】使用 Retrofit Android 使用 WCF,响应正文为空,响应代码 405【英文标题】:Consuming WCF using Retrofit Android, The Response Body is null and response code 405 【发布时间】:2017-05-22 03:51:23 【问题描述】:我有包含此方法的 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());
);
响应对象返回空正文和错误代码 405 的问题,谁能帮我解决这个问题?
这是原始响应:
响应protocol=http/1.1, code=405, message=Method Not Allowed, url=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
【问题讨论】:
@POST("/GetListInvititionFiltered") 在您的 intarface 中使用反斜杠。 我会检查它,但我将反斜杠放在 baseURL @Sorathiya Payal 【参考方案1】:您的 WCF 服务仅接受 GET
请求。但是AAPRestfulEndPointInterface
中的GetListInvititionFiltered
方法被声明为POST
。
@POST("GetListInvititionFiltered")
Call<InvitationsContainer> GetListInvititionFiltered(..);
将其更改为GET
以消除Method Not Allowed
错误。
@GET("GetListInvititionFiltered")
Call<InvitationsContainer> GetListInvititionFiltered(..);
【讨论】:
以上是关于使用 Retrofit Android 使用 WCF,响应正文为空,响应代码 405的主要内容,如果未能解决你的问题,请参考以下文章