gpt4 book ai didi

android - 通过 Web 服务将 ArrayList 从 Android 应用程序发送到远程数据库

转载 作者:行者123 更新时间:2023-11-29 14:55:23 26 4
gpt4 key购买 nike

我有一个包含特定值的“String”类型的 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));
}
}

}

最佳答案

要转换为 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();

...

关于android - 通过 Web 服务将 ArrayList 从 Android 应用程序发送到远程数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8067481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com