【问题标题】:Trouble Sending JSON from android to ASP.NET Web API将 JSON 从 android 发送到 ASP.NET Web API 时遇到问题
【发布时间】:2013-12-28 06:36:55
【问题描述】:

我正在尝试将一些 JSON 数据发送到这样的 asp.net web api 控制器(来自here):

  JSONObject Parent = new JSONObject();

        Parent.put("enterpriseId", "55e8a2a3-466d-46dc-95ce-bc5f2d3e7828");
        List<Integer> lst = new ArrayList<Integer>();
        lst.add(1);
        lst.add(2);
        lst.add(3);
        lst.add(4);
        lst.add(5);
        Parent.put("itemids", lst);

        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPostRequest = new HttpPost(Financial.AccountReviewUrl+"/testtesttest");

        StringEntity se;
        se = new StringEntity(Parent.toString());
        //Set HTTP parameters
        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json; charset=utf-8");
        httpPostRequest.setHeader("AuthenticationToken", Financial.UserEncrypt); 

        Log.d("json is", Parent.toString());
        Log.d("use encryp is", Financial.UserEncrypt);


        return httpclient.execute(httpPostRequest);

我的网络 API 操作:

        [HttpPost]
        public Object TestTestTest(Guid enterpriseId, List<int> itemIds)
        {
            var count = 0;
            if (itemIds != null)
                count = itemIds.Count;

            return enterpriseId.ToString() + ":" + count.ToString();

        }

而且我不断收到 404 错误。我知道这个问题之前已经被问过多次,但推荐的答案似乎都不适合我。有什么想法吗?

我看到 thisthis 和...回答但对我不起作用

【问题讨论】:

    标签: android json asp.net-web-api


    【解决方案1】:

    您的 API 需要一个表单帖子,而不是 JSON 对象。对于POSTing JSON 对象,您将执行以下操作

    // Code snippet for posting JSONObject
    HttpPost httpPost = new HttpPost(serviceUrl);
    MultipartEntity multipartEntity = new MultipartEntity();    
    multipartEntity.addPart("data", new StringBody(jsonObject.toString()));
    httpPost.setEntity(multipartEntity);
    try {
        HttpResponse response = httpClient.execute(httpPost);
    } catch (Exception ex) {
        // Log the error
    }
    

    由于您的代码需要 URL 编码的表单数据,您将按如下方式进行操作

    // Code snippet for posting form data
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(serviceUrl);
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    for (int i = 0; i < params.length; i++) {
        nameValuePairs.add(new BasicNameValuePair(params[i], values[i]));
    }
    UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(nameValuePairs);
    httpPost.setEntity(formEntity);
    try {
        HttpResponse response = httpClient.execute(httpPost);
    } catch (Exception ex) {
        // Log the error
    }
    

    在第二个代码 sn-p 中,您将构建两个NameValuePair 对象,一个用于enterpriseId,另一个用于itemids

    PS:这些是代码sn-ps,你必须根据你的使用来修改它们。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的回答,我做了第二种方法并且它有效,但我需要发送 Json。我尝试第一种方法,会告诉你结果
    • MultipartEntity 类是什么?
    • 好的。对于发送 JSON,我相信您也必须修改您的 API。
    • org.apache.http.entity.mime.MultipartEntity
    • 能否请您告诉我如何将一份清单发送给BasicNameValuePair
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-11
    • 2018-02-07
    • 2013-03-30
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多