2013-10-07 81 views
0

我已經寫在asp.net MVC服務,它接受字符串化JSON對象作爲參數,我想從我的Android代碼從Android的服務呼叫收到的JSONObject是在.NET服務

消費,這是服務空方法

public JsonResult saveDataInSession(string jsonObjSend) 
{ 
    JsonResult jsonResult = null; 
    List<OnlineUserDetails> lstValue = new List<OnlineUserDetails>(); 
    OnlineUserDetails UserDetails = new OnlineUserDetails(); 
    JavaScriptSerializer jss = new JavaScriptSerializer(); 
    UserDetails = JsonConvert.DeserializeObject<OnlineUserDetails>(jsonData); 
    UserDetails.LoginId = UserDetails.EmaillId; 
    UserLoginAndSignUp UserLoginAndSignUp = new UserLoginAndSignUp(); 
    string result = UserLoginAndSignUp.SaveDetails(UserDetails); 
    jsonResult = Json(UserDetails, JsonRequestBehavior.AllowGet); 
    return jsonResult; 
} 

這該是多麼我是從Android的

jsonObjSend = new JSONObject(); 
try{ 
    jsonObjSend.put("FirstName", firstname.getText().toString()); 
    jsonObjSend.put("LastName", lastname.getText().toString()); 
    jsonObjSend.put("EmaillId", emailid.getText().toString()); 
    jsonObjSend.put("Password", password.getText().toString()); 
    jsonObjSend.put("MobileNumber", mobilenumber.getText().toString()); 
    //jsonObjSend.put("Login_RememberMe", "true"); 
    // 
}catch (JSONException e) { 
    e.printStackTrace(); 
} 

發送JSON對象,使一個HttpRequest調用諸如

try { 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httpPostRequest = new HttpPost(URL); 

    StringEntity se=null; 
    try { 
     se = new StringEntity(jsonObjSend.toString(),HTTP.UTF_8);   
    } catch (UnsupportedEncodingException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    se.setContentType("application/json"); 
    se.setContentEncoding("UTF-8"); 
    // Set HTTP parameters 
    httpPostRequest.setEntity(se); 
    HttpResponse response = (HttpResponse) httpclient.execute(httpPostRequest); 

但是,每當我嘗試通過傳遞jsonobject消費webservice我收到空。

什麼是錯誤?

感謝您的幫助提前

回答

0

我認爲你需要設置的dataType = json的當JSON數據

在你的情況,我想有一個選項調用像這樣一起發佈要求:

se.setDataType("json"); 
0

您需要通過ByteArrayEntity,而不是StringEntity爲`httpPostRequest.setEntity()

請嘗試以下代碼:

HttpPost request = new HttpPost(url); 
    request.addHeader("Content-Type", "application/json"); 
    HttpResponse response; 
    try { 
     request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8"))); 
     response = mHttpClient.execute(request); 
     String jsonString = EntityUtils.toString(response.getEntity()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    }