本文介绍了使用翻新版发送带参数的发帖请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Retrofit库在Android上使用API​​失败,但是在使用POSTMAN时,我可以看到预期的结果.

I'm trying unsuccessfully to consume an API on Android using Retrofit library but while using POSTMAN I can see the expected results.

邮递员设置

  • api网址(基本+控制器)

  • The api url (base+controller)

HTTP方法设置为POST

HTTP Method set to POST

点击了源数据或x-www-form-urlencoded

Clicked the from-data or x-www-form-urlencoded

然后我将两个参数传递给键/值字段.

Then i pass the two params on the key/value fields.

Android改进设置

@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Query("code") String code,
                          @Query("monthact") String monthact,
                          Callback<LandingPageReport> cb);

@FormUrlEncoded
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
                          @Field("monthact") String monthact,
                          Callback<LandingPageReport> cb);

此选项均无效.但是结果是{}.

None of this options works. But am getting {} as result.

更新

使用标准的HttpClient(和HttpPost)类进行相同的设置可以正常工作.

Same settings by using the standard HttpClient(and HttpPost) class works fine.

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);

List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("code", "testcode"));
urlParameters.add(new BasicNameValuePair("monthact", "feb-2015"));

post.setEntity(new UrlEncodedFormEntity(urlParameters));

HttpResponse response = client.execute(post);

为什么我不能执行此请求并无法在Retrofit中获得正确的响应?

Why I can't do this request and get the correct response in Retrofit?

更新2

@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Query("code") String code,
                          @Query("monthact") String monthact,
                          Callback<List<LandingPageReport>> cb);

@FormUrlEncoded
@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
                          @Field("monthact") String monthact,
                          Callback<List<LandingPageReport>>> cb);

玩转之后,我认为我找到了问题的根源.我已经更新了改造代码,以接收List<LandingPageReport>.但是现在发生了这个错误

After playing around I think I've found the source of the problem. I've updated my retrofit code to receive List<LandingPageReport>. But now this error occur

原因是我消耗了2个api(webapi和wcf).我所有其他的json响应都是对象数组. [{},{}],但在这次通话中我收到了

The reason is that i consume 2 api's (webapi and wcf). All my other json response are arrays of objects. [{},{}] but in this call I've received this

{
  "GetDetailWithMonthWithCodeResult": [
     {
        "code": "test",
        "field1": "test",
     }
   ]
}

但是我仍然无法解析响应.

But still I can't manage to parse the response.

推荐答案

我找到了解决方案.这个问题是我的班级结构中的一个问题.所以我像以下示例一样更新了它们.

I have found the solution. The issue was a problem in my classes structure. So i updated them like the following samples.

public class LandingPageReport {

    private ArrayList<LandingPageReportItem> GetDetailWithMonthWithCodeResult;

    // + Getter Setter methods
}

public class LandingPageReportItem {

    private String code;

    private String field1;

    // + Getter Setter methods
}

然后我使用此改造配置

@POST("/GetDetailWithMonthWithCode")
void getLandingPageReport(@Field("code") String code,
                          @Field("monthact") String monthact,
                          Callback<LandingPageReport> cb);

这篇关于使用翻新版发送带参数的发帖请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:15
查看更多