安卓应用ASP。NET WebAPi服务器——发送复杂类型

时间:2022-08-12 21:12:20

I have a Android App which is via Web Services connected with my ASP.NET WebApi Server. For sending requests I am using AsyncHttpClient and for passing parameters to the server I use RequestParams. Now I want to send a complex object that contains a list with other complex objects to my server, and there is the issue. How am I supposed to do that? For simple types, it is pretty easy, since I just need to call parameter.add("paramname", paramvalue); multiple times on the same parameter name, and on the server side I receive a list. But what should the call look like when my list has complex types so that I receive a list on my server containing these complex types?

我有一个Android应用程序,通过Web服务连接到我的ASP。净WebApi服务器。对于发送请求,我使用的是AsyncHttpClient,对于向服务器传递参数,我使用的是RequestParams。现在,我想向我的服务器发送一个包含有其他复杂对象的列表的复杂对象,这里有一个问题。我该怎么做?对于简单类型,它非常简单,因为我只需要调用parameter。add(“paramname paramvalue);在相同的参数名上多次出现,在服务器端我收到一个列表。但是,当我的列表具有复杂类型时,当我在服务器上收到包含这些复杂类型的列表时,调用应该是什么样子呢?

Let's say I have a book that contains a list of authors:

假设我有一本包含作者列表的书:

class Book {
   string title;
   int year;
   List<Authors> authors;
}

class Author {
    string name;
    int    age;
}

How should I pass a Book (including authors)to my server using Request params (let's assume the data class on the server looks the same)?

如何使用请求解析将图书(包括作者)传递到服务器(假设服务器上的数据类看起来是相同的)?

I've almost forgot that the server accepts JSON as format.

我几乎忘记了服务器接受JSON作为格式。

Thanks

谢谢

1 个解决方案

#1


1  

  1. First you need to declare the Book and Author (POJO) in your android project.
  2. 首先,需要在android项目中声明该书和作者(POJO)。
  3. Then you need to convert your complex objects to json object (you can either do it manually or with GSON Libary)

    然后需要将复杂对象转换为json对象(可以手动或使用GSON Libary)

    Gson gson = new Gson();
    String jsonString = gson.toJson(myBook);
    JSONObject bookJsonObj = new JSONObject();
    
    try {
        bookJsonObj = new JSONObject(jsonString);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
  4. Then you can use library like Google Volley to post your request to server:

    然后您可以使用谷歌Volley这样的库将您的请求发送到服务器:

    JsonObjectRequest myJsonRequest = new JsonObjectRequest(Request.Method.POST,
                    ACTION_METHOD_URL, bookJsonObj , new Response.Listener<JSONObject>() {
    
        @Override
        public void onResponse(JSONObject jObj) {
    
        }
    }, new Response.ErrorListener() {
    
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    volleySingleton.addToRequestQueue(myJsonRequest, MY_REQUEST_TAG);
    
  5. Finally add Action method for the request in your api controller

    最后,在api控制器中为请求添加操作方法

    public IHttpActionResult YourActionMethod(Book book)    
    {
        // do your request action here
    }
    

EDIT: for posting JSON via AsyncHttpClient use this:

编辑:对于通过AsyncHttpClient发布JSON,请使用:

    StringEntity entity = new StringEntity(bookJsonObj.toString());
    client.post(context, ACTION_METHOD_URL, entity, "application/json",
            responseHandler)

#1


1  

  1. First you need to declare the Book and Author (POJO) in your android project.
  2. 首先,需要在android项目中声明该书和作者(POJO)。
  3. Then you need to convert your complex objects to json object (you can either do it manually or with GSON Libary)

    然后需要将复杂对象转换为json对象(可以手动或使用GSON Libary)

    Gson gson = new Gson();
    String jsonString = gson.toJson(myBook);
    JSONObject bookJsonObj = new JSONObject();
    
    try {
        bookJsonObj = new JSONObject(jsonString);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    
  4. Then you can use library like Google Volley to post your request to server:

    然后您可以使用谷歌Volley这样的库将您的请求发送到服务器:

    JsonObjectRequest myJsonRequest = new JsonObjectRequest(Request.Method.POST,
                    ACTION_METHOD_URL, bookJsonObj , new Response.Listener<JSONObject>() {
    
        @Override
        public void onResponse(JSONObject jObj) {
    
        }
    }, new Response.ErrorListener() {
    
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    volleySingleton.addToRequestQueue(myJsonRequest, MY_REQUEST_TAG);
    
  5. Finally add Action method for the request in your api controller

    最后,在api控制器中为请求添加操作方法

    public IHttpActionResult YourActionMethod(Book book)    
    {
        // do your request action here
    }
    

EDIT: for posting JSON via AsyncHttpClient use this:

编辑:对于通过AsyncHttpClient发布JSON,请使用:

    StringEntity entity = new StringEntity(bookJsonObj.toString());
    client.post(context, ACTION_METHOD_URL, entity, "application/json",
            responseHandler)