我有这样的json:
[
{
"bank":{
"central_office_address":"ddd",
"license":"12312312",
"location_id":3,
"name":"prior",
"tax_number":"12312312",
"year_of_foundation":1987
}
}
]
我尝试像这里解析它: http://p-xr.com/android-tutorial-how-to-parse-read-json-data-into-a-android-listview/
我在教育和学习测试方面做到这一点。
我的活动:
package com.example.tespapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView tvHello;
private static String url = "http://localhost/denwer/banks.json";
// JSON Node names
JSONArray contacts = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvHello = (TextView) findViewById(R.id.textView1);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View v){
tvHello.setText("Hello Kitty!");
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
}
}
JSONParser与下面的链接相同....
但是我无法解析甚至看不到它....点击我的应用程序崩溃(
在catlog中,我看到:
04-05 16:30:43.713: E/Trace(1218): error opening trace file: No such file or directory (2)
04-05 16:30:44.943: D/libEGL(1218): loaded /system/lib/egl/libEGL_emulation.so
04-05 16:30:44.953: D/(1218): HostConnection::get() New Host Connection established 0x2a149640, tid 1218
04-05 16:30:45.113: D/libEGL(1218): loaded /system/lib/egl/libGLESv1_CM_emulation.so
04-05 16:30:45.123: D/libEGL(1218): loaded /system/lib/egl/libGLESv2_emulation.so
04-05 16:30:45.433: W/EGL_emulation(1218): eglSurfaceAttrib not implemented
04-05 16:30:45.803: D/OpenGLRenderer(1218): Enabling debug mode 0
04-05 16:31:29.633: D/AndroidRuntime(1218): Shutting down VM
04-05 16:31:29.633: W/dalvikvm(1218): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
04-05 16:31:29.703: E/AndroidRuntime(1218): FATAL EXCEPTION: main
04-05 16:31:29.703: E/AndroidRuntime(1218): java.lang.IllegalStateException: Could not execute method of the activity
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.view.View$1.onClick(View.java:3599)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.view.View.performClick(View.java:4204)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.view.View$PerformClick.run(View.java:17355)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.os.Handler.handleCallback(Handler.java:725)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.os.Handler.dispatchMessage(Handler.java:92)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.os.Looper.loop(Looper.java:137)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 16:31:29.703: E/AndroidRuntime(1218): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-05 16:31:29.703: E/AndroidRuntime(1218): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-05 16:31:29.703: E/AndroidRuntime(1218): at dalvik.system.NativeStart.main(Native Method)
04-05 16:31:29.703: E/AndroidRuntime(1218): Caused by: java.lang.reflect.InvocationTargetException
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.lang.reflect.Method.invoke(Method.java:511)
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.view.View$1.onClick(View.java:3594)
04-05 16:31:29.703: E/AndroidRuntime(1218): ... 11 more
04-05 16:31:29.703: E/AndroidRuntime(1218): Caused by: android.os.NetworkOnMainThreadException
04-05 16:31:29.703: E/AndroidRuntime(1218): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-05 16:31:29.703: E/AndroidRuntime(1218): at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-05 16:31:29.703: E/AndroidRuntime(1218): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-05 16:31:29.703: E/AndroidRuntime(1218): at com.example.tespapp.JSONParser.getJSONFromUrl(JSONParser.java:38)
04-05 16:31:29.703: E/AndroidRuntime(1218): at com.example.tespapp.MainActivity.onClick(MainActivity.java:68)
04-05 16:31:29.703: E/AndroidRuntime(1218): ... 14 more
04-05 16:31:32.023: I/Process(1218): Sending signal. PID: 1218 SIG: 9
我也尝试其他json,其他链接......
答案 0 :(得分:1)
你的根本问题是:
android.os.NetworkOnMainThreadException
您无法在android中的UI线程上执行长时间运行的网络操作。操作系统阻止您这样做,这会阻止您首先获取json对象,从而导致抛出所有其他异常。
您需要创建一个AsyncTask(或其他类型的线程)来处理您的网络操作。然后,您创建该类的实例并在主线程中对其调用execute()方法。
以下是AsyncTasks
的文档答案 1 :(得分:1)
只是为了运行测试/代码:(不建议在实际项目中使用)
尝试在Android OS低于4+的模拟器/设备上测试代码,其中添加了不允许主UI线程上的连接!日志行Caused by: android.os.NetworkOnMainThreadException
真正的解决方案:
您需要添加AsyncTask
或Thread
并执行以下连接:
让它改变工作
public void onClick(View view){
new LongOperation().execute("");
}
还要添加一个内部类:
private class LongOperation extends AsyncTask<String, Void, JSONObject> {
@Override
protected JSONObject doInBackground(String... params) {
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
try {
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
} catch(Exception ex) { // many diffent exceptions better handle seperatelly
Log.e("error", "error", ex);
return null;
}
return json;
}
@Override
protected void onPostExecute(JSONObject result) {
// do smth with the result
String str = (json == null)?"json = null":json.toString(4);
tvHello.setText(str);
}
}