URLからJSONを取得するアプリを作成しています。最初はエミュレータを使って、ローカルホストの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);