1
我有一個android活動從servlet接收數組列表。示例[stl1010,P3]。當我嘗試將其轉換爲JSON對象(使用解析器類)時,我得到異常「解析數據org.json.JSONException時的錯誤:Value [」stl1010「,」P3「] org.json.JSONArray類型無法轉換到JSONObject「。下面是servlet代碼:將數組列表轉換爲JSON
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解析器類+活動)。活動代碼:
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解析器類代碼:
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;
}
}
好吧,如果你知道你從服務器發送一個JSONArray,爲什麼不簡單地分析它的客戶端上的陣列?而不是'JSONObject jObj =新的JSONObject(json)',你會'JSONArray jArr = new JSONArray(json)' –