1

Im gonna develope a Android app wich i have done few times before, but the trick now is that i need to access a database (not sqllite in the device). And we have done a website before that has all the functions that i need. So my thought was to use that website (MVC3) to get information from the database, and send information to the database from the App. Do you guys have some ideas for me how to code that? What i need is to recieve data from the website to the App with Json or Gson i guess, then i need to post up some data to the controllers from the App, dont know if i need to use url parameters or if i can use Jsom that way aswell?

Emil Örtberg
  • 75
  • 1
  • 10

2 Answers2

1

To send a json object in the ASP.NET Controller like: {"name":"foo","age":5}

The code of the Controller could be something like:

[HttpPost]
public JsonResult MyAction(UserViewModel UserModel)
{
    /* do something with your usermodel object */
    UserModel.Name = "foo";
    UserModel.Age = 5;

    return Json(UserModel, JsonRequestBehavior.AllowGet);
}

EDIT: On the Android side, here it is the method to send the request and receive the response:

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(); 
}
bre_dev
  • 511
  • 2
  • 15
1

You can use JSON requests. ASP.NET MVC 3 has a built-in JsonValueProvider which allows to deserialize JSON requests into strongly typed view models. For example let's suppose that you have the following model:

public class MyViewModel
{
    public string Name { get; set; }
    public int Age { get; set; }
}

and the following controller action:

[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
    ...
}

you could send the following POST request to it:

POST /mycontroller/myaction
Content-Length: 22
Content-Type: application/json; charset=UTF-8
Host: www.example.com

{"name":"foo","age":5}
Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876