【问题标题】:Sending an ArrayList from Android app to remote database via web service通过 Web 服务将 ArrayList 从 Android 应用程序发送到远程数据库
【发布时间】:2011-11-09 15:41:01
【问题描述】:

我有一个“字符串”类型的 ArrayList,其中包含某些值。我想使用 Web 服务将此 ArrayList 发送到远程数据库。我打算使用 JSON 来插入相同的内容,但我遇到了一些困难,想知道如何去做。

这是我目前的代码:

我的 ArrayList 是这样定义的。它的目的是存储复选框值

      ArrayList<String> items2 = new ArrayList<String>();

            for (int i = 0; i < arr2.length; i++) 
            {
                if (checkedabsent.get(i)) 
                {
                    items2.add(arr2[i]); //arr2 is a String array containing all values
                    System.out.println(items2);
                }
            }

            Log.d("", "items:");
            for (String string : items2)
            {
                Log.d("", string);
                System.out.println(items2);
            }

这就是我发现的困难! JSON 代码

   HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx");
   HttpClient client = new DefaultHttpClient(httpParams);

    try
            {
            String result;
            JSONObject obj = new JSONObject();
           // how do I put the array list over here ??           

            int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
            HttpParams httpParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);

            httppost.setHeader("Content-Type", "application/json");
            httppost.setHeader("Accept","application/json");

            // Execute HTTP Post Request
            HttpResponse httpResponse = client.execute(httppost);
            HttpEntity httpEntity = httpResponse.getEntity();


            if (httpEntity != null) 
            {
                InputStream is = httpEntity.getContent();
                result = is.toString();
                Log.i("Result is ", "Result: " + result);
                if(result.equalsIgnoreCase("success"))
                {
                    startActivity(new Intent(Mark.this,nextscreen.class));
                }
            }

            }

【问题讨论】:

  • 在这段代码中......好吧......一切都错了...... 1.您没有发布任何内容 2. is.toString() 不会返回 InputStream 内容,而是返回 InputStream 字符串表示形式。 .. 3. 你首先有HttpClient client = new DefaultHttpClient(httpParams);,然后是HttpParams httpParams = new BasicHttpParams(); 在try 范围内......有什么用?
  • 是的,这确实是我的代码中的一个主要缺陷,我接受它。后来我意识到了。我现在肯定已经删除了。感谢您的反馈!

标签: android json web-services http-post


【解决方案1】:

要转换为 JSON 数组,您可以使用以下代码

ArrayList<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
JSONArray jsArray = new JSONArray(list);

然后像described here一样传递jsArray

对不起,如果我误解了你的问题。

更新,所以:

  ArrayList<String> items2 = new ArrayList<String>();

        for (int i = 0; i < arr2.length; i++) 
        {
            if (checkedabsent.get(i)) 
            {
                items2.add(arr2[i]); //arr2 is a String array containing all values
                System.out.println(items2);
            }
        }

        Log.d("", "items:");
        for (String string : items2)
        {
            Log.d("", string);
            System.out.println(items2);
        }

        HttpPost httppost = new HttpPost("http://10.0.2.2/enterdata/Service1.asmx");
        HttpClient client = new DefaultHttpClient(httpParams);

        try
        {
            String result;
            //JSONObject obj = new JSONObject();
            JSONArray jsArray = new JSONArray(items2);

            int TIMEOUT_MILLISEC = 10000;  // = 10 seconds
            HttpParams httpParams = new BasicHttpParams();

...

【讨论】:

  • 你不必抱歉,伙计!所以这意味着我必须为每个检查项目编写 list.add() 语句,不是吗? .我可以放一个循环来添加列表中的所有项目吗?如果是这样,如何?以及就我的代码而言,我需要做哪些更改?
  • 您是否已经从您的代码中填充了 ArrayList:ArrayList&lt;String&gt; items2 = new ArrayList&lt;String&gt;();,所以您只需拥有 JSONArray jsArray = new JSONArray(items2); 并拥有您的 JSON 数组。
  • 是的,我明白了! :-) 我还想问的一件事是,一旦数组作为我的 Web 服务的参数,我总是可以在我的 WebService 代码中编写一个插入查询,以将数组“items2”的各个值放入我的数据库中,对吗?
  • 当然可以。 :) 你将把它当作你的网络服务中的一个数组,并用它做你喜欢做的事。 :)
【解决方案2】:

你能做到吗?

ArrayList<String> items2 = new ArrayList<String>();
...

JSONObject obj = new JSONObject();

for(int i=0; i<items2.size(); i++)
{
    obj.put("" + i, items.get(i));
}
//then use obj.write(Writer x) to send it

【讨论】:

  • 那我该如何使用 HttpPost 呢?我需要还是不需要?
  • 您可以随时在 JSONObject 上使用toString() 来获取其字符串表示形式
【解决方案3】:

您可以从您的ArrayList 创建一个JSONArray

JSONArray foo = new JSONArray(yourArrayList);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    相关资源
    最近更新 更多