私はjsonオブジェクトを使用してデータを解析しています.データはサーバーからも解析されています.コンソールには表示されますが、エミュレータではクラッシュしました..
これが私のコードです:
public class jsonUtil {
static HttpURLConnection urlConnection;
public static HttpURLConnection openConnection() throws Exception {
int responseCode = -1;
try {
URL url = new URL("url display");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-encoded");
urlConnection.setRequestProperty(
"Content-Length",
"" + Integer.toString("category=breaking_news&latitude=&longitude="
.getBytes().length));
urlConnection.setRequestProperty("Content-Language", "en-US");
urlConnection.setRequestProperty("Accept-Encoding", "identity");
urlConnection.setUseCaches(false);
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
urlConnection.setReadTimeout(10 * 1000);
urlConnection.setConnectTimeout(10 * 1000);
DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.writeBytes("category=breaking_news&latitude=&longitude=");
wr.flush();
wr.close();
responseCode = urlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
throw new Exception("Server not responding");
}
} catch (SocketException e) {
throw new Exception("Connection Time Out");
} catch (java.net.UnknownHostException unknownHostException) {
// TODO: handle exception
throw new Exception("unknownHostException");
} catch (Exception e) {
// TODO: handle exception
throw new Exception("Error Occured");
}
return urlConnection;
}
public static ArrayList<String> jsonParseList = new ArrayList<String>();
public static ArrayList<String> jsonParsing() {
StringBuffer buffer = new StringBuffer();
JSONArray array;
try {
try {
urlConnection = openConnection();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream()));
String line = bufferedReader.readLine();
while (line != null) {
buffer.append(line);
line = bufferedReader.readLine();
}
bufferedReader.close();
if (buffer.toString() != null) {
try {
array = new JSONArray(buffer.toString());
JSONObject jsonObject;
for (int i = 0; i < array.length(); i++) {
jsonObject = array.getJSONObject(i);
String conv_title = jsonObject.getString("conv_title");
String content = jsonObject.getJSONObject("first_post")
.getString("content");
String creator = jsonObject.getJSONObject("first_post")
.getJSONObject("creator").getString("name");
Log.e("List Values", conv_title);
jsonParseList.add(conv_title);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonParseList;
}
}
ログキャット:
' 06:35:21.637: E/List Activity(795): [] 05-30 06:35:21.687: W/Trace(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35:21.697: W/ Trace(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35:21.717: W/Trace(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35:21.927: I/Choreographer(795): スキップされました144コマ!(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35:22.158: I/Choreographer(795): 48 フレームをスキップしました! アプリケーションがメイン スレッドで処理しすぎている可能性があります。05-30 06:35:22.317: W/Trace(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35:22.317: W/Trace(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35 :22.337: W/Trace(795): nativeGetEnabledTags からの予期しない値: 0 05-30 06:35:23.485: D/dalvikvm(795): GC_CONCURRENT 解放された 276K、12% 解放された 2894K/3288K、