web-dev-qa-db-ja.com

Androidネットワークに到達できません-ENETUNREACH

Androidアプリケーションで奇妙なエラーが発生しています。Xamppを使用してローカルサーバーでテストしています。アプリはデータベースからレコードを取得し、電話に保存することです。

問題は、Motorola XT550でアプリをAndroid 2.3.6でテストし、Sony Xperia SでAndroid 4.0.4でアプリが動作することです。ただし、Android 4.1.2(私のクライアントの電話))でSamsung Fameを使用してテストすると、機能しません。

LogCatは次のとおりです。

02-22 04:48:33.001    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.Java:183)
02-22 04:48:33.001    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.Java:164)
02-22 04:48:33.001    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.Java:119)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.Java:360)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:670)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:509)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.Java:487)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at com.sistel.manantiales.SyncActivity$DatabaseSync.updateDatabase(SyncActivity.Java:198)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at com.sistel.manantiales.SyncActivity$DatabaseSync.doInBackground(SyncActivity.Java:165)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at com.sistel.manantiales.SyncActivity$DatabaseSync.doInBackground(SyncActivity.Java:147)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at Android.os.AsyncTask$2.call(AsyncTask.Java:287)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:305)
02-22 04:48:33.011    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.util.concurrent.FutureTask.run(FutureTask.Java:137)
02-22 04:48:33.021    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1076)
02-22 04:48:33.021    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:569)
02-22 04:48:33.021    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.lang.Thread.run(Thread.Java:856)
02-22 04:48:33.021    3961-3976/com.sistel.manantiales W/System.err﹕ Caused by: Java.net.ConnectException: failed to connect to /10.0.0.7 (port 80) after 15000ms: connect failed: ENETUNREACH (Network is unreachable)
02-22 04:48:33.021    3961-3976/com.sistel.manantiales W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.Java:114)
02-22 04:48:33.031    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:192)
02-22 04:48:33.031    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.net.PlainSocketImpl.connect(PlainSocketImpl.Java:459)
02-22 04:48:33.031    3961-3976/com.sistel.manantiales W/System.err﹕ at Java.net.Socket.connect(Socket.Java:842)
02-22 04:48:33.031    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.Java:119)
02-22 04:48:33.031    3961-3976/com.sistel.manantiales W/System.err﹕ at org.Apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.Java:144)
02-22 04:48:33.031    3961-3976/com.sistel.manantiales W/System.err﹕ ... 15 more
02-22 04:48:33.041    3961-3976/com.sistel.manantiales W/System.err﹕ Caused by: libcore.io.ErrnoException: connect failed: ENETUNREACH (Network is unreachable)
02-22 04:48:33.081    1216-1467/? V/AudioFlinger﹕ presentationComplete() session 35 complete: framesWritten 940032
02-22 04:48:33.081    1216-1467/? V/AudioFlinger﹕ TrackBase::reset
02-22 04:48:33.091    3961-3976/com.sistel.manantiales W/System.err﹕ at libcore.io.Posix.connect(Native Method)
02-22 04:48:33.091    3961-3964/com.sistel.manantiales D/dalvikvm﹕ GC_CONCURRENT freed 197K, 13% free 7645K/8775K, paused 16ms+16ms, total 54ms
02-22 04:48:33.091    3961-3976/com.sistel.manantiales W/System.err﹕ at libcore.io.BlockGuardOs.connect(BlockGuardOs.Java:85)
02-22 04:48:33.091    3961-3976/com.sistel.manantiales W/System.err﹕ at libcore.io.IoBridge.connectErrno(IoBridge.Java:144)
02-22 04:48:33.101    3961-3976/com.sistel.manantiales W/System.err﹕ at libcore.io.IoBridge.connect(IoBridge.Java:112)
02-22 04:48:33.101    3961-3976/com.sistel.manantiales W/System.err﹕ ... 20 more
02-22 04:48:33.101    1577-1800/? D/PowerManagerService﹕ acquireDVFSLockLocked : type : DVFS_MIN_LIMIT  frequency : 999999  uid : 1000  pid : 1577  tag : ActivityManager
02-22 04:48:33.101    1577-1800/? W/ActivityManager﹕ mDVFSLock.acquire()
02-22 04:48:33.141    1577-1800/? D/BatteryStatsImpl﹕ ++++++xy WakeLock start : pid=1577, name=, type=0, tid=1800, pid=1577, pName=null
02-22 04:48:33.161    1577-1588/? D/WindowManager﹕ PhoneWindowManager: focusChangedLw
02-22 04:48:33.161    1577-1588/? D/WindowManager﹕ updateReadingMode
02-22 04:48:33.161    1577-1588/? D/KeyguardViewMediator﹕ setHidden false
02-22 04:48:33.161    1577-1588/? D/WindowManager﹕ mInputFocus is not null.
02-22 04:48:33.161    1577-1588/? D/WindowManager﹕ mInputFocus is not null.
02-22 04:48:33.161    1577-1588/? D/WindowManager﹕ mInputFocus is not null.
02-22 04:48:33.191    3961-3961/com.sistel.manantiales D/AbsListView﹕ Get MotionRecognitionManager
02-22 04:48:33.211    3961-3976/com.sistel.manantiales W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x411ba2a0)
02-22 04:48:33.211    1577-1810/? D/WindowManager﹕ mInputFocus is not null.
02-22 04:48:33.211    3961-3976/com.sistel.manantiales E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
        Java.lang.RuntimeException: An error occured while executing doInBackground()
        at Android.os.AsyncTask$3.done(AsyncTask.Java:299)
        at Java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.Java:273)
        at Java.util.concurrent.FutureTask.setException(FutureTask.Java:124)
        at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:307)
        at Java.util.concurrent.FutureTask.run(FutureTask.Java:137)
        at Java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.Java:1076)
        at Java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.Java:569)
        at Java.lang.Thread.run(Thread.Java:856)
        Caused by: Java.lang.IllegalArgumentException: HTTP entity may not be null
        at org.Apache.http.util.EntityUtils.toString(EntityUtils.Java:110)
        at org.Apache.http.util.EntityUtils.toString(EntityUtils.Java:146)
        at com.sistel.manantiales.SyncActivity$DatabaseSync.updateDatabase(SyncActivity.Java:217)
        at com.sistel.manantiales.SyncActivity$DatabaseSync.doInBackground(SyncActivity.Java:165)
        at com.sistel.manantiales.SyncActivity$DatabaseSync.doInBackground(SyncActivity.Java:147)
        at Android.os.AsyncTask$2.call(AsyncTask.Java:287)
        at Java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.Java:305)

そして、ここに私のコードがあります:

public class DatabaseSync extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... url) {

            try {
                clientDataSource.open();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                movementDataSource.open();
            } catch (SQLException e) {
                e.printStackTrace();
            }

            clientDataSource.deleteAllClients();
            updateDatabase(url[0]);

            return null;
        }

        @Override
        protected void onPostExecute(Boolean bool) {
            super.onPostExecute(bool);
        }

        public void updateDatabase(String url) {

                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("Operation", "Download"));
                nameValuePairs.add(new BasicNameValuePair("Value", "Download"));

                HttpParams httpParams = new BasicHttpParams();

                HttpConnectionParams.setConnectionTimeout(httpParams, 15000);
                HttpConnectionParams.setSoTimeout(httpParams, 15000);

            HttpClient httpClient = new DefaultHttpClient(httpParams);
            HttpPost httpPost = new HttpPost(url);

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            HttpResponse httpResponse = null;

            try {
                httpResponse = httpClient.execute(httpPost);
            } catch (IOException e) {
                e.printStackTrace();
            }

            HttpEntity httpEntity = null;

            try {
                httpEntity = httpResponse.getEntity();
            }catch (NullPointerException e) {

                Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(intent);

            }


            String result = "empty";
            try {
                result = EntityUtils.toString(httpEntity);
            } catch (IOException e) {
                e.printStackTrace();
            }

            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(result);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            JSONArray jsonClients = null;

            try {
                jsonClients =  jsonObject.getJSONArray("clients");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            long id = 0;
            String name;
            String address;
            String locality;
            long bonus = 0;
            double price = 0.0;
            double prev_balance = 0.0;
            double balance = 0.0;
            long prev_bottles = 0;
            long bottles = 0;
            String update_date;
            String sync_date;

            for(int i = 0; i < jsonClients.length(); i++) {
                JSONObject json = null;
                try {
                    json = jsonClients.getJSONObject(i);

                    String s_id = new String(Base64.decode(json.get("id").toString(),Base64.DEFAULT));
                    name = new String(Base64.decode(json.get("name").toString(),Base64.DEFAULT));
                    address = new String(Base64.decode(json.get("address").toString(), Base64.DEFAULT));
                    locality = new String(Base64.decode(json.get("locality").toString(), Base64.DEFAULT));
                    String s_bonus = new String(Base64.decode(json.get("bonus").toString(), Base64.DEFAULT));
                    String s_price = new String(Base64.decode(json.get("price").toString(), Base64.DEFAULT));
                    String s_prev_balance = new String(Base64.decode(json.get("prev_balance").toString(), Base64.DEFAULT));
                    String s_balance = new String(Base64.decode(json.get("balance").toString(), Base64.DEFAULT));
                    String s_prev_bottles = new String(Base64.decode(json.get("prev_bottles").toString(), Base64.DEFAULT));
                    String s_bottles = new String(Base64.decode(json.get("bottles").toString(), Base64.DEFAULT));
                    update_date = new String(Base64.decode(json.get("update_date").toString(), Base64.DEFAULT));
                    sync_date = new String(Base64.decode(json.get("sync_date").toString(), Base64.DEFAULT));

                    id = Long.parseLong(s_id);
                    bonus = Long.parseLong(s_bonus);
                    price = Double.parseDouble(s_price);
                    prev_balance = Double.parseDouble(s_prev_balance);
                    balance = Double.parseDouble(s_balance);
                    prev_bottles = Long.parseLong(s_prev_bottles);
                    bottles = Long.parseLong(s_bottles);

                    Client client = new Client(id, name, address, locality, bonus, price,
                            prev_balance, balance, prev_bottles, bottles, update_date, sync_date);

                    clientDataSource.insertClient(client);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
            startActivity(intent);

        }

    }
17

何が問題なのかがわかりました! Samsung FameはWi-Fiに接続されていませんでした。インターネットがありませんでした。

51

ユーザーがインターネットがそこにないことを知ることができるように、リクエストを行う前にインターネット接続のチェックを追加することもできます。

以下のコードを使用し、falseが返された場合、Toast/Alertを表示します。

private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager 
      = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
8
Swetank

私のアプリが接続しているときに、wifi> app crashをオフにすると、catchコマンド例外の内部に入ることができない場合があります。

1
Tran Huu Phuoc

デバイスのインターネット/ WiFi接続を確認してください

0
Raj K