我有一个从 servlet 接收数组列表的 android 活动。示例 [stl1010, P3]。当我尝试将其转换为 JSON 对象(使用解析器类)时,出现异常“解析数据 org.json.JSONException 时出错:org.json.JSONArray 类型的值 ["stl1010","P3"] 无法转换为JSON对象。” 下面是servlet代码:
private ArrayList<String> ticketLookup(String tkt)
{
Connection con = null;
Statement st = null;
//StringArray msga = new StringArray();
ArrayList<String> msga = new ArrayList();
try
{
// These will vary depending on your server/database
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = DriverManager.getConnection("jdbc:sqlserver://serveripaddr;databaseName=dbname;user=sa;password=pwd");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("Select status,description,priority,openingdate,closeddate,assigned_to,resolvergroup,assigneddatel1,impactedbyuser,emailid from ServiceRequest where TicketNo = '"+tkt+"'");
while (rs.next())
{
// rs.getString(2);
msga.add(String.valueOf(rs.getString("assigned_to")));
msga.add(String.valueOf(rs.getString("priority")));
}
//JSONArray mJSONArray = new JSONArray(Arrays.asList(mStringArray));
return msga;
下面是来自我的 android 应用程序的代码(JSON Parser 类 + 活动)。活动代码:
public void addListenerOnGoButton(){
btnGo.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
String url = "http://10.0.2.2:8080/GnP22/GetNpostServlet?TicketNo="+etTkt.getText();
String TAG_TICKET = "ticket";
String TAG_STATUS = "status";
String TAG_DESC = "description";
String TAG_PRIORITY = "priority";
String TAG_OPENDATE = "openingdate";
String TAG_CLOSEDATE = "closingdate";
String TAG_ASSIGNEDTO = "assignedto";
String TAG_RESOLVERGROUP = "resolvergroup";
String TAG_ASSIGNEDDATEL1 = "assigneddatel1";
String TAG_IMPACTEDUSER = "impactedbyuser";
String TAG_EMAILID = "emailid";
// contacts JSONArray
JSONArray contacts = null;
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_TICKET);
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(0);
String status = c.getString(TAG_STATUS);
String description = c.getString(TAG_DESC);
//String email = c.getString(TAG_EMAIL);
//String address = c.getString(TAG_ADDRESS);
//String gender = c.getString(TAG_GENDER);
tvStatus.setText(status);
tvDescription.setText(description);
}
} catch (JSONException e) {
//e.printStackTrace();
Log.d("JSON Exception", e.toString());
}
}
});
}
下面是 JSON Parser 类的代码:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
//HttpPost httpPost = new HttpPost(url);
HttpGet httpPost = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}