Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To send a json object in the ASP.NET Controller like: {"name":"foo","age":5}</p> <p>The code of the Controller could be something like:</p> <pre><code>[HttpPost] public JsonResult MyAction(UserViewModel UserModel) { /* do something with your usermodel object */ UserModel.Name = "foo"; UserModel.Age = 5; return Json(UserModel, JsonRequestBehavior.AllowGet); } </code></pre> <p>EDIT: On the Android side, here it is the method to send the request and receive the response:</p> <pre><code>public void GetDataFromServer() { ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); 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&lt;NameValuePair&gt; 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(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload