MainActivity
代码:
public class MainActivity extends Activity {
private static final String TAG_CONACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_MOBILE = "mobile";
private static final String TAG_HOME_PHONE = "home";
private JSONObject jObject;
private JSONArray jArray;
private TextView txt;
private JSONArray contacts;
private String url = "http://api.androidhive.info/contacts/";
private JSONObject json;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StringBuilder builder = new StringBuilder();
;
txt = (TextView) findViewById(R.id.txt);
JSONparser jsoNparser = new JSONparser();
json = jsoNparser.getJSONFromUrl(url);
try {
jArray=json.getJSONArray(TAG_CONACTS);
for (int i = 0; i < jArray.length(); i++) {
jObject=jArray.getJSONObject(i);
Log.e(JSONparser.class.getName(),"ID : "+ jObject.getString(TAG_ID));
Log.e(JSONparser.class.getName(),"NAME : "+ jObject.getString(TAG_NAME));
Log.e(JSONparser.class.getName(),"EMAIL : "+ jObject.getString(TAG_EMAIL));
Log.e(JSONparser.class.getName(),"ADDRESS : "+ jObject.getString(TAG_ADDRESS));
Log.e(JSONparser.class.getName(),"GENDER : "+ jObject.getString(TAG_GENDER));
JSONObject phone=jObject.getJSONObject(TAG_PHONE);
Log.e(JSONparser.class.getName(),"MOBILE : "+ phone.getString(TAG_MOBILE));
Log.e(JSONparser.class.getName(),"HOME PHONE : "+ phone.getString(TAG_HOME_PHONE));
}
} catch (JSONException e) {
Log.e(JSONparser.class.getName(),e.toString());
}
}
JSONparser 类公共类 JSONparser {
private InputStream instream;
private StringBuilder stringBuilder;
private JSONObject jSonoj = null;
// contructor
public JSONparser() {
}
public JSONObject getJSONFromUrl(String url) {
// get JSON
try {
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
instream = client.execute(httpPost).getEntity().getContent();
} catch (Exception e) {
Log.e(MainActivity.class.getName(), e.toString());
}
// Write JSON
try {
stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(
instream));
String Line = null;
while ((Line = reader.readLine()) != null) {
stringBuilder.append(Line + "\n");
}
instream.close();
} catch (Exception e) {
Log.e(MainActivity.class.getName(), e.toString());
}
// parse JSON
try {
jSonoj = new JSONObject(stringBuilder.toString());
} catch (Exception e) {
Log.e(MainActivity.class.getName(), e.toString());
}
if (jSonoj != null)
Log.e(JSONparser.class.getName(), "SUCCESS");
return jSonoj;
}
}
它在 android 2.2 上运行良好
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): SUCCESS
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): ID : c200
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): NAME : Ravi Tamada
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): EMAIL : ravi@gmail.com
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): ADDRESS : xx-xx-xxxx,x - street, x - country
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): GENDER : male
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): MOBILE : +91 0000000000
06-12 01:37:59.968: E/sample.jsondemo.JSONparser(1619): HOME PHONE : 00 000000
当我在 4.2 上运行时出错
06-02 13:44:06.761: E/sample.jsondemo.MainActivity(1272): android.os.NetworkOnMainThreadException
06-02 13:44:06.761: E/sample.jsondemo.MainActivity(1272): java.lang.NullPointerException
06-02 13:44:06.773: E/sample.jsondemo.MainActivity(1272): org.json.JSONException: End of input at character 0 of
06-02 13:44:06.781: D/AndroidRuntime(1272): Shutting down VM
06-02 13:44:06.781: W/dalvikvm(1272): threadid=1: thread exiting with uncaught exception (group=0xa62b9288)
06-02 13:44:06.797: E/AndroidRuntime(1272): FATAL EXCEPTION: main
06-02 13:44:06.797: E/AndroidRuntime(1272): java.lang.RuntimeException: Unable to start activity ComponentInfo{sample.jsondemo/sample.jsondemo.MainActivity}: java.lang.NullPointerException
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.os.Handler.dispatchMessage(Handler.java:99)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.os.Looper.loop(Looper.java:137)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-02 13:44:06.797: E/AndroidRuntime(1272): at java.lang.reflect.Method.invokeNative(Native Method)
06-02 13:44:06.797: E/AndroidRuntime(1272): at java.lang.reflect.Method.invoke(Method.java:511)
06-02 13:44:06.797: E/AndroidRuntime(1272): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-02 13:44:06.797: E/AndroidRuntime(1272): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-02 13:44:06.797: E/AndroidRuntime(1272): at dalvik.system.NativeStart.main(Native Method)
06-02 13:44:06.797: E/AndroidRuntime(1272): Caused by: java.lang.NullPointerException
06-02 13:44:06.797: E/AndroidRuntime(1272): at sample.jsondemo.MainActivity.onCreate(MainActivity.java:44)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.Activity.performCreate(Activity.java:5008)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-02 13:44:06.797: E/AndroidRuntime(1272): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-02 13:44:06.797: E/AndroidRuntime(1272): ... 11 more
我希望这个代码在每个版本上运行,请帮忙。谢谢大家。 链接源代码