2014-03-24 73 views
0

我一直在尋找這個問題的幾個小時,但我似乎無法找到任何答案的任何地方的WebAPI POST,服務總是接收空

我已經建立了一個ASP.NET的WebAPI接受JSON GET/POST請求當我使用谷歌瀏覽器的提琴手或高級休息客戶端擴展時,它工作A1。

現在我必須採取現有的Android應用程序,並修改它與我的WebAPI一起工作。我已經清除了所有垃圾,我不需要簡化它,但仍然無法發佈。 GET工作正常,我收到我的響應字符串,並且一切正常,但POST返回400錯誤請求。

我的webApi控制器需要一個ProblemReports對象。 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; } 

} 

與子類

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; } 
} 

public class contact_information 
{ 
    public String name { get; set; } 
    public String phone { get; set; } 
} 

下面是我在我的服務處理程序類 的Android POST代碼有趣的是,我可以在將我的json字符串發送到StringEntity之前放置斷點,複製原始json字符串,將其粘貼到BODY部分高級休息區nt,填寫標題,打回帖和景氣:響應是200 OK

如果我斷點控制器上我的WebAPI,我可以看到,通過高級休息客戶端發送請求時,我可以訪問problemreports,但如果我休息它在我的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; 
} 

這是從我的應用程序生成的原始JSON字符串。實際的字符串工作正常提琴手或其他高級客戶

{ 
"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" 
} 

}

我不知道這到底是怎麼回事理念上,任何幫助/提示/建議將盡。 我已經把我的控制器拋出任何錯誤,我唯一得到的是400錯誤的請求

這裏我控制器爲

// 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); 
    } 
+0

你能分享WebApi控制器的相關部分? – Bill

+0

完成,編輯我的問題! – Tascalator

+2

發現問題,我的StringEntity應該是 StringEntity se = new StringEntity(json,「UTF-8」); 因爲我需要等待8個小時纔回答我自己的問題,所以我明天發佈這個答案作爲答案。謝謝你的時間 – Tascalator

回答

4

柱部分找到了答案,我的問題。了4個小時,發現小錯誤,但這裏有雲:

我行

httpPost.setHeader("Content-type", "application/json; charset=utf-8") 

和string實體

StringEntity se = new StringEntity(json); 

它需要的是

httpPost.setHeader("Content-type", "application/json") 

並且

StringEntity se = new StringEntity(json, "UTF-8"); 

繁榮一切工作正常,抱歉打擾任何人感謝閱讀,並有美好的一天