Tas*_*tor 0 c# asp.net android json asp.net-web-api
我已经为这个问题搜索了几个小时,但似乎在任何地方都找不到任何答案
我已经构建了一个 ASP.NET WebAPI,它接受 JSON GET/POST 请求,当我使用 fiddler 或 google chrome 的高级休息客户端扩展时,它可以工作 A1。
现在我必须采用现有的 android 应用程序并修改它以与我的 WebAPI 一起使用。我已经清除了所有不需要的垃圾,但我仍然无法发布。GET 工作正常,我收到我的响应字符串,一切正常,但 POST 返回 400 Bad Request。
我的 webApi 控制器需要一个 ProblemReports 对象。问题报告是由我制作的,它是这样的:
public class ProblemReports
{
[Key]
public int problem_id { get; set; }
[Required]
public Nullable<int> request_type { get; set; }
[Required]
public Nullable<int> problem_type_id { get; set; }
public String picture_url { get; set; }
public contact_information contact_information { get; set; }
public problem_location problem_location { get; set; }
public bool dog_on_property { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
带子类
public class problem_location
{
public String street { get; set; }
public String city { get; set; }
public int relative_position_id { get; set; }
public double longitude { get; set; }
public double latitude { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
和
public class contact_information
{
public String name { get; set; }
public String phone { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这是我的服务处理程序类中我的 android POST 的代码 有趣的部分是我可以在将我的 json 字符串发送到 StringEntity 之前放置一个断点,复制原始 json 字符串,将其粘贴到 BODY 部分高级休息客户端中,填写标题,点击帖子和繁荣:响应为 200 OK
如果我在控制器上断点我的 WebAPI,我可以看到当通过 Advanced rest 客户端发送请求时,我可以访问问题报告,但是如果我在我的 android 应用程序的请求上中断它,问题报告为空
public static String POST(String url, ProblemReports report) {
InputStream inputStream = null;
String result = "";
if (report.picture_url == null)
{
report.picture_url = "no picture";
}
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("request_type", report.request_type);
jsonObject.accumulate("problem_type_id", report.problem_type_id);
jsonObject.accumulate("picture_url", report.picture_url);
JSONObject contact_informationObject = new JSONObject();
contact_informationObject.accumulate("name",
report.contact_information.name);
contact_informationObject.accumulate("phone",
report.contact_information.phone);
jsonObject.accumulate("contact_information",
contact_informationObject);
JSONObject problem_locationObject = new JSONObject();
problem_locationObject.accumulate("street",
report.problem_location.street);
problem_locationObject.accumulate("city",
report.problem_location.city);
problem_locationObject.accumulate("latitude",
report.problem_location.latitude);
problem_locationObject.accumulate("longitude",
report.problem_location.longitude);
problem_locationObject.accumulate("relative_position_id",
report.problem_location.relative_position_id);
jsonObject.accumulate("problem_location", problem_locationObject);
jsonObject.accumulate("dog_on_property", report.dog_on_property);
json = jsonObject.toString();
//String otherJson = "{ProblemReports: " + json + "}";
//I saw on the web to add ProblemReports: but it doensn't work
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json; charset=utf-8");
httpPost.setHeader(
"Authorization",
"Bearer TokenRemovedBecauseUseless");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "InputStream convert fail!!";
} catch (Exception e) {
return e.getCause().toString();
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
这是由我的应用程序制作的原始 JSON 字符串。该实际字符串在 Fiddler 或高级休息客户端上运行良好
{
"contact_information":
{
"phone":"6666666666",
"name":"fiber"
},
"dog_on_property":false,
"problem_type_id":3,
"request_type":1,
"problem_location":
{
"longitude":1234,
"latitude":1234,
"relative_position_id":0,
"city":"Montreal, QC A1A 1A1",
"street":"0000 René-Lévesque Blvd"
}
Run Code Online (Sandbox Code Playgroud)
}
我不知道这里发生了什么,任何帮助/提示/建议都可以。我已经设置我的控制器抛出任何错误,我唯一得到的是 400 BAD REQUEST
这里是我的控制器的帖子部分
// POST api/ProblemReports
[ResponseType(typeof(ProblemReports))]
public IHttpActionResult PostProblemReports([FromBody]ProblemReports problemreports)
{
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
if (!ModelState.IsValid)
{
throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState));
// return BadRequest(ModelState);
}
try
{
db.ProblemReports.Add(problemreports);
db.SaveChanges();
}
catch (Exception ex)
{
return Ok(ex);
}
//return CreatedAtRoute("DefaultApi", new { id = problemreports.problem_id }, problemreports);
ReturnID ri = new ReturnID(problemreports.problem_id);
return Ok(ri);
}
Run Code Online (Sandbox Code Playgroud)
找到了我的问题的答案。花了 4 个小时才找到那个小错误,但结果如下:
我有这条线
httpPost.setHeader("Content-type", "application/json; charset=utf-8")
Run Code Online (Sandbox Code Playgroud)
和字符串实体
StringEntity se = new StringEntity(json);
Run Code Online (Sandbox Code Playgroud)
它必须是
httpPost.setHeader("Content-type", "application/json")
Run Code Online (Sandbox Code Playgroud)
和
StringEntity se = new StringEntity(json, "UTF-8");
Run Code Online (Sandbox Code Playgroud)
Boom 一切正常,抱歉打扰任何人,感谢阅读,祝您有美好的一天
| 归档时间: |
|
| 查看次数: |
3606 次 |
| 最近记录: |