我可以连接到服务器并获取 json 数据。它是这种格式:
{"clazz":"ManiaContestantList","contestants":[
{"clazz":"ManiaContestant","contestantId":"1","contestantName":"Adira","photoUrl":"/fileFeed.action?service=astroManiaService&action=viewFile&type=JPG&path=1/1_CONTESTANT_PHOTO.png"},
{"clazz":"ManiaContestant","contestantId":"2","contestantName":"Akim","photoUrl":"/fileFeed.action?service=astroManiaService&action=viewFile&type=JPG&path=2/2_CONTESTANT_PHOTO.png"},
.
.
.
]}
我的代码是这样的:
@Override
protected void onStart() {
super.onStart();
Log.i("NewsList", "inside onStart();");
try {
URL url = new URL("/MY URL/");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(Manager.ConnTimeout);
conn.setReadTimeout(Manager.ReadTimeout);
int responseCode = conn.getResponseCode();
Log.i("Connection oppened", "Response code is:" + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
if (in != null) {
StringBuilder strBuilder = new StringBuilder();
// Read character by character
int ch = 0;
while ((ch = in.read()) != -1)
strBuilder.append((char) ch);
// get returned message and show it
strServerResponseMsg = strBuilder.toString();
Log.i("Data returned by server:", strServerResponseMsg);
JSONArray jObjects = new JSONArray(strServerResponseMsg);
for(int i=0; i<jObjects.length(); i++){
System.out.println(jObjects.getJSONObject(i).getString("contestantId").toString());
System.out.println(jObjects.getJSONObject(i).getString("contestantName").toString());
System.out.println(jObjects.getJSONObject(i).getString("photoUrl").toString());
}
in.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
我在“strServerResponseMsg”变量中有我的响应 Json。但在“ JSONArray jObjects = new JSONArray(strServerResponseMsg);
”行中,Logcat 显示typemismatch
错误。
请告诉我我的问题是什么?