Чтобы отправить объект json в контроллер ASP.NET, например: {"name": "foo", "age": 5}
Код контроллера может быть примерно таким:
[HttpPost]
public JsonResult MyAction(UserViewModel UserModel)
{
/* do something with your usermodel object */
UserModel.Name = "foo";
UserModel.Age = 5;
return Json(UserModel, JsonRequestBehavior.AllowGet);
}
РЕДАКТИРОВАТЬ: на стороне Android, вот способ отправки запроса и получения ответа:
public void GetDataFromServer() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("data_1", "data"));
nameValuePairs.add(new BasicNameValuePair("data_n", "data"));
try {
HttpPost httppost = new HttpPost("http://path_to_the_controller_on_server");
String result = RetrieveDataFromHttpRequest(httppost,nameValuePairs);
// parse json data
JSONObject jObjRoot = new JSONObject(result);
String objName = jObjRoot.getString("Name");
String objName = jObjRoot.getString("Age");
} catch (JSONException e) {
Log.e(TAG, "Error parsing data " + e.toString());
}
}
private String RetrieveDataFromHttpRequest(HttpPost httppost, ArrayList<NameValuePair> nameValuePairs ) {
StringBuilder sb = new StringBuilder();
try {
HttpClient httpclient = new DefaultHttpClient();
if (nameValuePairs != null)
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
// convert response to string
BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"));
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sb.toString();
}