【发布时间】: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