我正在尝试使用android连接到我的在线数据库。按照教程后,我想出了这段代码:
package com.example.helloandroid;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.*;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class HelloAndroid extends Activity {
JSONArray jArray;
String result = null;
InputStream is = null;
StringBuilder sb=null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.1.4/~jasptack/Software%20engineering/connector.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
//parse json data
try{
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
Log.i("log_tag","x-co: "+json_data.getInt("x-coordinaat")+
", straat: "+json_data.getString("straat")
);
}
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
}
执行此操作时,出现以下错误: 12-04 23:25:07.711:E / log_tag(353):解析数据时出错org.json.JSONException:Value
有人可以帮忙吗?谢谢!
答案 0 :(得分:0)
如果JSON字符串格式错误,则会发生这种情况。确保String
result
是有效的JSONArray
(应以'['开头)。此外,您可以尝试将result
映射到JSONObject
。如果这些都没问题,JSONArray
中的一个元素就会格格不入。
答案 1 :(得分:0)
我曾尝试解析我的Web服务返回的JSON字符串。这是我做的:
我从网络服务获得的回复是:
{ “checkrecord”:[{ “rollno”: “ABC2”, “百分比”:40, “出席”:12, “遗漏”:34}], “表1”:[]}
为了解析我做了以下事情:
JSONObject jsonobject = new JSONObject(result);
JSONArray array = jsonobject.getJSONArray("checkrecord");
int max = array.length();
for (int j = 0; j < max; j++)
{
JSONObject obj = array.getJSONObject(j);
JSONArray names = obj.names();
for (int k = 0; k < names.length(); k++)
{
String name = names.getString(k);
String value= obj.getString(name);
}
我的JSONObject如下所示:
{ “表1”:[], “checkrecord”:[{ “遗漏”:34, “出席”:12, “百分比”:40 “rollno”: “ABC2”}]}
这是@ 500865试图建议的内容。我刚给你一个代码示例。首先检查您的结果并确定它是否有效。还要检查JSONArray结果。 如果可能的话,可以在这里发布。
希望有所帮助
干杯