从 Android 应用程序获取/发布数据到 MVC3 网站和反之亦然

发布于 2025-01-07 12:36:12 字数 246 浏览 0 评论 0原文

我要开发一个 Android 应用程序,我之前已经做过几次了,但现在的技巧是我需要访问数据库(而不是设备中的 sqllite)。而且我们之前做过一个网站,有我需要的所有功能。所以我的想法是使用该网站(MVC3)从数据库获取信息,并将信息从应用程序发送到数据库。 你们对我如何编写代码有一些想法吗?我需要的是用 Json 或 Gson 将数据从网站接收到应用程序,然后我需要从应用程序将一些数据发布到控制器,不知道我是否需要使用 url 参数或者是否可以使用 Jsom也是这样吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深海少女心 2025-01-14 12:36:12

要在 ASP.NET 控制器中发送 json 对象,如下所示:
{"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(); 
}

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(); 
}
吾性傲以野 2025-01-14 12:36:12

您可以使用 JSON 请求。 ASP.NET MVC 3 有一个内置的 JsonValueProvider,它允许将 JSON 请求反序列化为强类型视图模型。例如,假设您有以下模型:

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

和以下控制器操作:

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

您可以向其发送以下 POST 请求:

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

{"name":"foo","age":5}

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文