我正在构建一个从 URL 检索 JSON 的应用程序。起初,我使用模拟器,并在 localhost 上连接 URL。(它工作得很好)然后我想从外部服务器访问 URL。现在有问题了。
logcat 总是这样:
05-17 15:36:48.049: W/System.err(330): java.net.UnknownHostException: culigui.16mb.com
05-17 15:36:48.049: W/System.err(330): at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
05-17 15:36:48.049: W/System.err(330): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
05-17 15:36:48.059: W/System.err(330): at java.net.InetAddress.getAllByName(InetAddress.java:242)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-17 15:36:48.059: W/System.err(330): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-17 15:36:48.069: W/System.err(330): at last.project.CuliGUI.JSONParser.getJSONFromUrl(JSONParser.java:30)
05-17 15:36:48.069: W/System.err(330): at last.project.CuliGUI.MenuPromotion.onCreate(MenuPromotion.java:54)
05-17 15:36:48.069: W/System.err(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 15:36:48.069: W/System.err(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-17 15:36:48.069: W/System.err(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-17 15:36:48.069: W/System.err(330): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-17 15:36:48.069: W/System.err(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-17 15:36:48.069: W/System.err(330): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 15:36:48.069: W/System.err(330): at android.os.Looper.loop(Looper.java:123)
05-17 15:36:48.069: W/System.err(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-17 15:36:48.080: W/System.err(330): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 15:36:48.080: W/System.err(330): at java.lang.reflect.Method.invoke(Method.java:521)
05-17 15:36:48.080: W/System.err(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-17 15:36:48.080: W/System.err(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-17 15:36:48.080: W/System.err(330): at dalvik.system.NativeStart.main(Native Method)
05-17 15:36:48.080: E/Buffer Error(330): Error converting result java.lang.NullPointerException
05-17 15:36:48.089: E/JSON Parser(330): Error parsing data org.json.JSONException: End of input at character 0 of
05-17 15:36:48.089: D/AndroidRuntime(330): Shutting down VM
05-17 15:36:48.099: W/dalvikvm(330): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-17 15:36:48.118: E/AndroidRuntime(330): FATAL EXCEPTION: main
05-17 15:36:48.118: E/AndroidRuntime(330): java.lang.RuntimeException: Unable to start activity ComponentInfo{last.project.CuliGUI/last.project.CuliGUI.MenuPromotion}: java.lang.NullPointerException
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-17 15:36:48.118: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 15:36:48.118: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:521)
05-17 15:36:48.118: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-17 15:36:48.118: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-17 15:36:48.118: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
05-17 15:36:48.118: E/AndroidRuntime(330): Caused by: java.lang.NullPointerException
05-17 15:36:48.118: E/AndroidRuntime(330): at last.project.CuliGUI.MenuPromotion.onCreate(MenuPromotion.java:58)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-17 15:36:48.118: E/AndroidRuntime(330): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
我注意到 java.net.UnknownHostException 有问题。我读过一些关于它的文章,但老实说我不明白。
这是我的活动,以防你们中的一些人需要它:
// connect to external server
private static String url = "http://culigui.16mb.com/getdataresto.php";
ListView Listview;
LazyAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_view_all);
// Hashmap for ListView
ArrayList<HashMap<String, String>> userList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting JSONArray of listresto
JSONArray listresto = json.getJSONArray("listresto");
// looping through All listresto
for(int i = 0; i < listresto.length(); i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject list = listresto.getJSONObject(i);
// insert String to Local Variable
map.put("KEY_ID", list.getString("id_resto"));
map.put("KEY_NAME", list.getString("nama_resto"));
map.put("KEY_ADDRESS", list.getString("alamat_resto"));
map.put("KEY_THUMB", list.getString("thumb_img"));
map.put("KEY_ABOUT", list.getString("about_resto"));
map.put("KEY_PHONE", list.getString("no_telp"));
map.put("KEY_LAT", list.getString("loc_lat"));
map.put("KEY_LONG", list.getString("loc_long"));
userList.add(map);
//for checking value
//System.out.println("output: " +map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
//this is new custom adapter
Listview = (ListView) findViewById (R.id.list);
adapter = new LazyAdapter(this, userList);
Listview.setAdapter(adapter);