1

我正在使用 php android 练习 Connection 示例,该示例是通过 JSON 通信显示数据库中的名称和图片列表。

我发现的问题如下:

假设我的 PHP 或数据库位于以下地址:

http://example.com/phpdirection

我的应用程序连接到互联网,一切都很好。现在假设我的数据库被删除了,或者我的PHP的外壳被删除了,那么分配的地址:http://example.com/phpdirection

停止运营。

通过测试我发现了以下错误:

10-01 15:33:38.290: W/System.err(23332): java.net.UnknownHostException: Unable to resolve host "motaink.hol": No address associated with hostname
10-01 15:33:38.322: W/System.err(23332):    at java.net.InetAddress.lookupHostByName(InetAddress.java:478)
10-01 15:33:38.322: W/System.err(23332):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:243)
10-01 15:33:38.322: W/System.err(23332):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-01 15:33:38.329: W/System.err(23332):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-01 15:33:38.329: W/System.err(23332):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-01 15:33:38.329: W/System.err(23332):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-01 15:33:38.329: W/System.err(23332):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-01 15:33:38.329: W/System.err(23332):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-01 15:33:38.329: W/System.err(23332):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-01 15:33:38.337: W/System.err(23332):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-01 15:33:38.337: W/System.err(23332):    at com.prueba.bibliotecaappprueba.JSONParser.makeHttpRequest(JSONParser.java:62)
10-01 15:33:38.337: W/System.err(23332):    at com.prueba.bibliotecaappprueba.LisCategAventActivity$Activity.doInBackground(LisCategAventActivity.java:122)
10-01 15:33:38.337: W/System.err(23332):    at com.prueba.bibliotecaappprueba.LisCategAventActivity$Activity.doInBackground(LisCategAventActivity.java:1)
10-01 15:33:38.337: W/System.err(23332):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-01 15:33:38.337: W/System.err(23332):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-01 15:33:38.337: W/System.err(23332):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-01 15:33:38.337: W/System.err(23332):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-01 15:33:38.337: W/System.err(23332):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-01 15:33:38.337: W/System.err(23332):    at java.lang.Thread.run(Thread.java:856)
10-01 15:33:38.337: W/System.err(23332): Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
10-01 15:33:38.337: W/System.err(23332):    at libcore.io.Posix.getaddrinfo(Native Method)
10-01 15:33:38.337: W/System.err(23332):    at libcore.io.ForwardingOs.getaddrinfo(ForwardingOs.java:55)
10-01 15:33:38.337: W/System.err(23332):    at java.net.InetAddress.lookupHostByName(InetAddress.java:463)
10-01 15:33:38.337: W/System.err(23332):    ... 18 more
10-01 15:33:38.345: E/Buffer Error(23332): Error converting result java.lang.NullPointerException
10-01 15:33:38.345: E/JSON Parser(23332): Error parsing data org.json.JSONException: End of input at character 0 of 
10-01 15:33:38.345: W/dalvikvm(23332): threadid=11: thread exiting with uncaught exception (group=0x40a9f210)
10-01 15:33:38.345: E/AndroidRuntime(23332): FATAL EXCEPTION: AsyncTask #1
10-01 15:33:38.345: E/AndroidRuntime(23332): java.lang.RuntimeException: An error occured while executing doInBackground()
10-01 15:33:38.345: E/AndroidRuntime(23332):    at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.lang.Thread.run(Thread.java:856)
10-01 15:33:38.345: E/AndroidRuntime(23332): Caused by: java.lang.NullPointerException
10-01 15:33:38.345: E/AndroidRuntime(23332):    at com.prueba.bibliotecaappprueba.LisCategAventActivity$Activity.doInBackground(LisCategAventActivity.java:125)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at com.prueba.bibliotecaappprueba.LisCategAventActivity$Activity.doInBackground(LisCategAventActivity.java:1)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-01 15:33:38.345: E/AndroidRuntime(23332):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
10-01 15:33:38.345: E/AndroidRuntime(23332):    ... 4 more
10-01 15:33:38.790: E/WindowManager(23332): Activity com.prueba.bibliotecaappprueba.LisCategAventActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42367020 that was originally added here
10-01 15:33:38.790: E/WindowManager(23332): android.view.WindowLeaked: Activity com.prueba.bibliotecaappprueba.LisCategAventActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@42367020 that was originally added here
10-01 15:33:38.790: E/WindowManager(23332):     at android.view.ViewRootImpl.<init>(ViewRootImpl.java:355)
10-01 15:33:38.790: E/WindowManager(23332):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:267)
10-01 15:33:38.790: E/WindowManager(23332):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
10-01 15:33:38.790: E/WindowManager(23332):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
10-01 15:33:38.790: E/WindowManager(23332):     at android.view.Window$LocalWindowManager.addView(Window.java:537)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.Dialog.show(Dialog.java:284)
10-01 15:33:38.790: E/WindowManager(23332):     at com.prueba.bibliotecaappprueba.LisCategAventActivity$Activity.onPreExecute(LisCategAventActivity.java:113)
10-01 15:33:38.790: E/WindowManager(23332):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:561)
10-01 15:33:38.790: E/WindowManager(23332):     at android.os.AsyncTask.execute(AsyncTask.java:511)
10-01 15:33:38.790: E/WindowManager(23332):     at com.prueba.bibliotecaappprueba.LisCategAventActivity.onCreate(LisCategAventActivity.java:61)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.Activity.performCreate(Activity.java:4539)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2013)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2083)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.ActivityThread.access$600(ActivityThread.java:134)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1233)
10-01 15:33:38.790: E/WindowManager(23332):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-01 15:33:38.790: E/WindowManager(23332):     at android.os.Looper.loop(Looper.java:137)
10-01 15:33:38.790: E/WindowManager(23332):     at android.app.ActivityThread.main(ActivityThread.java:4697)
10-01 15:33:38.790: E/WindowManager(23332):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 15:33:38.790: E/WindowManager(23332):     at java.lang.reflect.Method.invoke(Method.java:511)
10-01 15:33:38.790: E/WindowManager(23332):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
10-01 15:33:38.790: E/WindowManager(23332):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
10-01 15:33:38.790: E/WindowManager(23332):     at dalvik.system.NativeStart.main(Native Method)

应用程序关闭。在这种情况下我能做什么?如果我想用消息“数据库中的问题”显示另一个活动,谢谢!

那是我的代码:

public class LisCategAventActivity extends Activity {

    private ProgressDialog pDialog;
    public int i = 0;
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> DaftarRS = new ArrayList<HashMap<String, String>>();

    private static String url_daftar_rs = "http://example.com/phpdirection";

    public static final String TAG_SUCCESS = "success";
    public static final String TAG_DAFTAR_RS = "daftar_rs";
    public static final String TAG_ID_RS = "id_rs";
    public static final String TAG_NOMBRE_RS = "nombre_rs";
    public static final String TAG_LINK_IMAGE_RS = "link_image_rs";
    public static final String TAG_AUTOR_RS = "autor_rs";
    public static final String TAG_CATEG_RS = "categ_rs";
    public static final String TAG_LINK_DESCARGA_RS = "link_descarga_rs";

    JSONArray daftar_rs = null;

    ListView list;
    ListAdapter adapter;
    private LisCategAventActivity activity;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cat_av);

        DaftarRS = new ArrayList<HashMap<String, String>>();

        new Activity().execute();

        activity = this;
        list = (ListView) findViewById(R.id.list_cat_av);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                String id_rs = ((TextView) view
                        .findViewById(R.id.id_rs)).getText().toString();

                Intent in = new Intent(getApplicationContext(),
                        DetallesActivity.class);

                in.putExtra(TAG_ID_RS, id_rs);

                startActivityForResult(in, 100);
            }
        });


    }

    public void SetListViewAdapter(ArrayList<HashMap<String, String>> daftar) {
        adapter = new ListAdapter(activity, daftar);
        list.setAdapter(adapter);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == 100) {

            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }

    class Activity extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LisCategAventActivity.this);
            pDialog.setMessage("Cargando Biblioteca...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {

            Conexion();
            if (i == 0){
                List<NameValuePair> params = new ArrayList<NameValuePair>();

                JSONObject json = jParser.makeHttpRequest(url_daftar_rs, "GET",
                        params);

                Log.d("All Products: ", json.toString());

                try {

                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {

                        daftar_rs = json.getJSONArray(TAG_DAFTAR_RS);

                        for (int i = 0; i < daftar_rs.length(); i++) {
                            JSONObject c = daftar_rs.getJSONObject(i);

                            String id_rs = c.getString(TAG_ID_RS);
                            String nama_rs = c.getString(TAG_NOMBRE_RS);
                            String link_image_rs = c
                                    .getString(TAG_LINK_IMAGE_RS);
                            String alamat_rs = c.getString(TAG_AUTOR_RS);
                            String telepon_rs = c.getString(TAG_CATEG_RS);

                            HashMap<String, String> map = new HashMap<String, String>();

                            map.put(TAG_ID_RS, id_rs);
                            map.put(TAG_NOMBRE_RS, nama_rs);
                            map.put(TAG_LINK_IMAGE_RS, link_image_rs);
                            map.put(TAG_AUTOR_RS, alamat_rs);
                            map.put(TAG_CATEG_RS, telepon_rs);

                            DaftarRS.add(map);
                        }
                    } else {

                    }
                } catch (JSONException e) {
                    Intent nohay= new Intent(LisCategAventActivity.this, noActivity.class);
                    onStop();   
                    i = 1;
                     startActivity(nohay);
                    e.printStackTrace();
                }
            }
            else{finish();}


            return null;
        }

        protected void onPostExecute(String file_url) {

            pDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {

                    SetListViewAdapter(DaftarRS);

                }
            });

        }

    } }
4

1 回答 1

1

尝试在logcat上显示id_rs、nama_rs等内容,使用androidLog类,或者直接使用System.out.println(),检查是否为null。

您应该进行如下检查:

if (json!=null) {                          // this is from 'JSONObject json = ...'
   //parse json and insert data to list
} else {                                   //json==null
   //display `database error`
}

希望这有帮助,祝你好运

里德

于 2013-10-02T06:58:06.077 回答