サーバーに接続して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
エラーを示しています。
私の問題は何ですか?