Android应用程序与ASP.NET的WebAPI服务器 - 发送复杂类型 [英] Android App with ASP.NET WebAPi Server - send complex types

查看:136
本文介绍了Android应用程序与ASP.NET的WebAPI服务器 - 发送复杂类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,它可通过与我的ASP.NET的WebAPI服务器连接的Web服务。对于发送请求我使用 AsyncHttpClient 和参数传递给我用 RequestParams 服务器。现在,我想送一个复杂的对象,它包含与其他复杂的对象到我的服务器列表,并没有问题。我怎么做呢?对于简单的类型,它是pretty容易,因为我只需要调用 parameter.add(PARAMNAME,paramvalue); 在同一个参数名多次,而在服务器端我收到的清单。但是,应该在调用的时候看我的列表中有复杂的类型,这样我收到一封含有这些复杂的类型我的服务器列表怎么样?

让我们说我有一本书,包含了作者的列表:

 类Book {
   字符串称号;
   INT年;
   清单<&作者GT;作者;
}类作者{
    字符串名称;
    INT年龄;
}

我应该如何通过使用请求参数一书(包括作者)到我的服务器(我们假设数据类的服务器看起来是一样的)?

我差点忘了服务器接受JSON作为格式。

感谢


解决方案

  1. 首先,你需要在你的Andr​​oid项目申报书和作者(POJO)。

  2. 然后,你需要你的复杂的对象转换成JSON对象(可以手动或使用GSON Libary做)

      GSON GSON =新GSON();
    字符串jsonString = gson.toJson(myBook);
    JSONObject的bookJsonObj =新的JSONObject();尝试{
        bookJsonObj =新的JSONObject(jsonString);
    }赶上(JSONException E){
        e.printStackTrace();
    }


  3. 然后你可以使用图书馆像谷歌抽射您的要求发布到服务器:

      JsonObjectRequest myJsonRequest =新JsonObjectRequest(Request.Method.POST,
                    ACTION_METHOD_URL,bookJsonObj,新Response.Listener<&JSONObject的GT;(){    @覆盖
        公共无效onResponse(JSONObject的jObj){    }
    },新Response.ErrorListener(){    @覆盖
        公共无效onErrorResponse(VolleyError错误){    }
    });
    volleySingleton.addToRequestQueue(myJsonRequest,MY_REQUEST_TAG);


  4. 最后添加操作方法的要求在API控制器

     公共IHttpActionResult YourActionMethod(书书)
    {
        //做你的要求这里的行动
    }


编辑:通过AsyncHttpClient张贴JSON使用:

  StringEntity实体=新StringEntity(bookJsonObj.toString());
    client.post(背景下,ACTION_METHOD_URL,实体,应用/ JSON
            ResponseHandler所)

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?

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.

Thanks

解决方案

  1. First you need to declare the Book and Author (POJO) in your android project.
  2. Then you need to convert your complex objects to json object (you can either do it manually or with 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();
    }
    

  3. Then you can use library like Google Volley to post your request to server:

    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);
    

  4. Finally add Action method for the request in your api controller

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

EDIT: for posting JSON via AsyncHttpClient use this:

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

这篇关于Android应用程序与ASP.NET的WebAPI服务器 - 发送复杂类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆