サーバーに接続してjsonデータを取得できます。それがこの形式である:私は "strServerResponseMsg" 変数で私にresponsed JSONを持ってAndroid、JSONの解析中キャッチエラー
@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();
}
}
:
{"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"},
.
.
.
]}
私のコードは次のようです。ライン "JSONArray jObjects = new JSONArray(strServerResponseMsg);
" Logcatはtypemismatch
エラーを示しています。
私の問題は何ですか?
ありがとうございました。私はそれを行い、配列を変更しました:JSONArray contestantObjects =新しいJSONArray((Collection)jObject.getJSONArray( "contestants"));走った後にクラッシュが起こった。あなたは何か考えていますか? – Hesam
JSONArrayを試すcontestantObjects = jObject.getJSONArray( "contestants");アクセスのための はhttp://stackoverflow.com/questions/1568762/jsonarray-with-javaを参照してください –