以下のデータは URL にあります: JSon を使用して解析してコース コードを読み取る方法: COMPSCI 101、提供される学期: サマー スクール、学期 1、学期 2 コースのタイトル: プログラミングの原則...... ...等々??
[{"codeField":"COMPSCI 101","semesterField":"サマー スクール; セメスター 1; セメスター 2","titleField":"プログラミングの原則"},{"codeField":"COMPSCI 105","semesterField" :"サマー スクール; 1 学期; 2 学期","titleField":"コンピューター サイエンスの原則"},{"codeField":"COMPSCI 111/111G","semesterField":"サマー スクール; 1 学期; 2 学期" ,"titleField":"サイバースペースをマスターする: 実用的なコンピューティングの紹介"}]
このコードを試してみましたが、エミュレーターで実行時に「残念ながら JsonParsing が機能しなくなった」という例外がスローされます。私はアンドロイドが初めてなので、私の素朴な間違いを許してください:package com.example.jsonparsing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class JsonActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_json);
setListAdapter(new ArrayAdapter(
this,android.R.layout.simple_list_item_1,
this.populate()));
}
private ArrayList<String> populate() {
ArrayList<String> items = new ArrayList<String>();
try {
URL url = new URL
("http://redsox.tcs.auckland.ac.nz/734A/CSService.svc/courses");
HttpURLConnection urlConnection =
(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
// gets the server json data
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String next;
while ((next = bufferedReader.readLine()) != null){
JSONArray ja = new JSONArray(next);
for (int i = 0; i < ja.length(); i++) {
JSONObject jo = (JSONObject) ja.get(i);
items.add(jo.getString("text"));
}
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return items;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_json, menu);
return true;
}
}
また、これは私のlogcatです:
05-11 12:25:34.151: I/Process(77): Sending signal. PID: 722 SIG: 3
05-11 12:25:34.161: I/dalvikvm(722): threadid=3: reacting to signal 3
05-11 12:25:34.231: I/dalvikvm(722): Wrote stack traces to '/data/anr/traces.txt'
05-11 12:25:34.281: D/AndroidRuntime(722): Shutting down VM
05-11 12:25:34.281: W/dalvikvm(722): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-11 12:25:34.291: E/AndroidRuntime(722): FATAL EXCEPTION: main
05-11 12:25:34.291: E/AndroidRuntime(722): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jsonparsing/com.example.jsonparsing.JsonActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.os.Looper.loop(Looper.java:137)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-11 12:25:34.291: E/AndroidRuntime(722): at java.lang.reflect.Method.invokeNative(Native Method)
05-11 12:25:34.291: E/AndroidRuntime(722): at java.lang.reflect.Method.invoke(Method.java:511)
05-11 12:25:34.291: E/AndroidRuntime(722): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-11 12:25:34.291: E/AndroidRuntime(722): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-11 12:25:34.291: E/AndroidRuntime(722): at dalvik.system.NativeStart.main(Native Method)
05-11 12:25:34.291: E/AndroidRuntime(722): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-11 12:25:34.291: E/AndroidRuntime(722): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:254)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.Activity.setContentView(Activity.java:1835)
05-11 12:25:34.291: E/AndroidRuntime(722): at com.example.jsonparsing.JsonActivity.onCreate(JsonActivity.java:26)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.Activity.performCreate(Activity.java:4465)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-11 12:25:34.291: E/AndroidRuntime(722): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-11 12:25:34.291: E/AndroidRuntime(722): ... 11 more
05-11 12:25:34.301: W/ActivityManager(77): Force finishing activity com.example.jsonparsing/.JsonActivity
05-11 12:25:34.301: W/WindowManager(77): Failure taking screenshot for (180x300) to layer 21010
05-11 12:25:34.574: I/Process(77): Sending signal. PID: 722 SIG: 3
05-11 12:25:34.574: I/dalvikvm(722): threadid=3: reacting to signal 3
05-11 12:25:34.581: I/dalvikvm(722): Wrote stack traces to '/data/anr/traces.txt'
05-11 12:25:34.825: W/ActivityManager(77): Activity pause timeout for ActivityRecord{41b22010 com.example.jsonparsing/.JsonActivity}
05-11 12:25:34.831: I/Process(77): Sending signal. PID: 722 SIG: 3
05-11 12:25:34.831: I/dalvikvm(722): threadid=3: reacting to signal 3
05-11 12:25:34.851: I/dalvikvm(722): Wrote stack traces to '/data/anr/traces.txt'
05-11 12:25:35.041: W/NetworkManagementSocketTagger(77): setKernelCountSet(10040, 0) failed with errno -2
05-11 12:25:45.058: W/ActivityManager(77): Activity destroy timeout for ActivityRecord{41b22010 com.example.jsonparsing/.JsonActivity}