(Logcat) Socket Exception: No Route to host

1.7k Views Asked by At

This is my Login.java

public class Login extends ActionBarActivity implements OnClickListener {

private Button login, register;
private EditText email, password;

JSONArray loginposition = null;
// Progress Dialog
private ProgressDialog pDialog;

// JSON parser class
JSONParser jsonParser = new JSONParser();

private static final String LOGIN_URL = "http://XXX.XXX.X.XX:1234/PMSS/login.php";


// JSON element ids from repsonse of php script:
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
private static final String TAG_POSTS = "posts";
private static final String TAG_EMAIL = "email";
private static final String TAG_POSITION = "position";

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    login = (Button) findViewById(R.id.login);
    register = (Button) findViewById(R.id.registerlauncher);
    email = (EditText) findViewById(R.id.useridlogin);
    password = (EditText) findViewById(R.id.passwordlogin);

    login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String Username = email.getText().toString();
            String Password = password.getText().toString();
            new AttemptLogin(Username, Password).execute();
        }
    });

    register.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(Login.this, Register.class);
            startActivity(intent);

        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // For the main activity, make sure the app icon in the action bar
        // does not behave as a button
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    /*
     * case R.id.login: new AttemptLogin().execute(); break; case
     * R.id.register: Intent i = new Intent(Login.this, Register.class);
     * startActivity(i); break;
     */
    default:
        break;
    }
}

class AttemptLogin extends AsyncTask<String, String, Integer> {

    private final String TAG = null;
    boolean failure = false;
    String res;
    String Username, Password,Email,Position;
    String CompareUser = "user", CompareStaff = "staff";
    int success;

    public AttemptLogin(String Username, String Password) {
        this.Username = Username;
        this.Password = Password;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Login.this);
        pDialog.setMessage("Attempting login...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();

    }

    protected Integer doInBackground(String... args) {

        try {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("username", Username));
            params.add(new BasicNameValuePair("password", Password));

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                    params);
            // check your log for json response
            Log.d("Login attempt", json.toString());

            // json success tag
            success = json.getInt(TAG_SUCCESS);
            res = json.getString(TAG_MESSAGE);

            if(success == 1){
            loginposition = json.getJSONArray(TAG_POSTS);
                // // looping through all posts according to the json object
                // // returned
                for (int i = 0; i < loginposition.length(); i++) {
                    // String jsonText = loginposition.getString(i);
                    JSONObject c = loginposition.getJSONObject(i);
                    // gets the content of each tag
                    // String content = c.getString(TAG_USERID);
                    Email = c.getString(TAG_EMAIL);
                    Position = c.getString(TAG_POSITION);

                }
            }

        } catch (JSONException e) {
            Log.e(TAG, "JSON error", e);
            success = Integer.valueOf(0);
        }
        return success;

    }

    protected void onPostExecute(Integer success) {
        // dismiss the dialog once product deleted
        pDialog.dismiss();

        if (success != null && success == 1) {
            Log.d("Login Successful!", "res: " + res);
            // save user data
            SharedPreferences sp = PreferenceManager
                    .getDefaultSharedPreferences(Login.this);
            Editor edit = sp.edit();
            edit.putString("email", Email);
            edit.putString("position", Position);
            edit.commit();
            if (Position.equals(CompareUser)) {
                Intent usermenu = new Intent(Login.this, MainMenu.class);
                startActivity(usermenu);
            }
            else if (Position.equals(CompareStaff)) {
                Intent staffmenu = new Intent(Login.this,
                        StaffMainMenu.class);
                startActivity(staffmenu);
            }
            Toast.makeText(
                    Login.this,
                    res == null ? "Please enter both user id and password  (success)"
                            : res, Toast.LENGTH_LONG).show();
            email.setText(null);
            password.setText(null);
        } else {
            Log.d("Login Failure!", "res: " + res);
            Toast.makeText(
                    Login.this,
                    res == null ? "Please enter both user id and password  (failed)"
                            : res, Toast.LENGTH_LONG).show();
        }

    }
}
}

This is my logcat

12-17 17:39:15.289: W/System.err(2643): java.net.SocketException: No route to host
12-17 17:39:15.289: W/System.err(2643):     at org.apache.harmony.luni.platform.OSNetworkSystem.connect(Native Method)
12-17 17:39:15.289: W/System.err(2643):     at dalvik.system.BlockGuard$WrappedNetworkSystem.connect(BlockGuard.java:357)
12-17 17:39:15.289: W/System.err(2643):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:204)
12-17 17:39:15.289: W/System.err(2643):     at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:437)
12-17 17:39:15.299: W/System.err(2643):     at java.net.Socket.connect(Socket.java:1002)
12-17 17:39:15.299: W/System.err(2643):     at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
12-17 17:39:15.299: W/System.err(2643):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143)
12-17 17:39:15.309: W/System.err(2643):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
12-17 17:39:15.309: W/System.err(2643):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
12-17 17:39:15.319: W/System.err(2643):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:359)
12-17 17:39:15.319: W/System.err(2643):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
12-17 17:39:15.319: W/System.err(2643):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-17 17:39:15.319: W/System.err(2643):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
12-17 17:39:15.319: W/System.err(2643):     at com.pmss.JSONParser.makeHttpRequest(JSONParser.java:110)
12-17 17:39:15.319: W/System.err(2643):     at com.pmss.Login$AttemptLogin.doInBackground(Login.java:164)
12-17 17:39:15.319: W/System.err(2643):     at com.pmss.Login$AttemptLogin.doInBackground(Login.java:1)
12-17 17:39:15.319: W/System.err(2643):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-17 17:39:15.319: W/System.err(2643):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-17 17:39:15.319: W/System.err(2643):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-17 17:39:15.329: W/System.err(2643):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-17 17:39:15.329: W/System.err(2643):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-17 17:39:15.329: W/System.err(2643):     at java.lang.Thread.run(Thread.java:1019)
12-17 17:39:15.329: E/Buffer Error(2643): Error converting result java.lang.NullPointerException
12-17 17:39:15.329: E/JSON Parser(2643): Error parsing data org.json.JSONException: End of input at character 0 of 
12-17 17:39:15.339: W/dalvikvm(2643): threadid=9: thread exiting with uncaught exception (group=0x40018578)
12-17 17:39:15.339: E/AndroidRuntime(2643): FATAL EXCEPTION: AsyncTask #1
12-17 17:39:15.339: E/AndroidRuntime(2643): java.lang.RuntimeException: An error occured while executing doInBackground()
12-17 17:39:15.339: E/AndroidRuntime(2643):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.lang.Thread.run(Thread.java:1019)
12-17 17:39:15.339: E/AndroidRuntime(2643): Caused by: java.lang.NullPointerException
12-17 17:39:15.339: E/AndroidRuntime(2643):     at com.pmss.Login$AttemptLogin.doInBackground(Login.java:167)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at com.pmss.Login$AttemptLogin.doInBackground(Login.java:1)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-17 17:39:15.339: E/AndroidRuntime(2643):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-17 17:39:15.339: E/AndroidRuntime(2643):     ... 4 more
12-17 17:39:21.429: E/WindowManager(2643): Activity com.pmss.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4055e320 that was originally added here
12-17 17:39:21.429: E/WindowManager(2643): android.view.WindowLeaked: Activity com.pmss.Login has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4055e320 that was originally added here
12-17 17:39:21.429: E/WindowManager(2643):  at android.view.ViewRoot.<init>(ViewRoot.java:263)
12-17 17:39:21.429: E/WindowManager(2643):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-17 17:39:21.429: E/WindowManager(2643):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-17 17:39:21.429: E/WindowManager(2643):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-17 17:39:21.429: E/WindowManager(2643):  at android.app.Dialog.show(Dialog.java:241)
12-17 17:39:21.429: E/WindowManager(2643):  at com.pmss.Login$AttemptLogin.onPreExecute(Login.java:150)
12-17 17:39:21.429: E/WindowManager(2643):  at android.os.AsyncTask.execute(AsyncTask.java:391)
12-17 17:39:21.429: E/WindowManager(2643):  at com.pmss.Login$1.onClick(Login.java:78)
12-17 17:39:21.429: E/WindowManager(2643):  at android.view.View.performClick(View.java:2485)
12-17 17:39:21.429: E/WindowManager(2643):  at android.view.View$PerformClick.run(View.java:9080)
12-17 17:39:21.429: E/WindowManager(2643):  at android.os.Handler.handleCallback(Handler.java:587)
12-17 17:39:21.429: E/WindowManager(2643):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-17 17:39:21.429: E/WindowManager(2643):  at android.os.Looper.loop(Looper.java:130)
12-17 17:39:21.429: E/WindowManager(2643):  at android.app.ActivityThread.main(ActivityThread.java:3687)
12-17 17:39:21.429: E/WindowManager(2643):  at java.lang.reflect.Method.invokeNative(Native Method)
12-17 17:39:21.429: E/WindowManager(2643):  at java.lang.reflect.Method.invoke(Method.java:507)
12-17 17:39:21.429: E/WindowManager(2643):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
12-17 17:39:21.429: E/WindowManager(2643):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
12-17 17:39:21.429: E/WindowManager(2643):  at dalvik.system.NativeStart.main(Native Method)

In the logcat it said No Route to host what does this means? I try open the URL in my browser everythings works fine, just that when my android application trying to connect it, then it forced close my application.

1

There are 1 best solutions below

5
On

here explanation :

Your 192.168.1.10 address is a DHCP address (dynamically) assigned by your router to your PC. As a result it is visible to the emulator.

But depending on how your WiFi is set up, the Android is probably on a different router / subnet and can't see 192.168.1.10. You could check that by typing 192.168.1.10 into the address bar of the Android browser.

If 192.168.1.10 is not visible to your Android, then you will have to either change the routing rules on your router to make it visible, or add a DNS lookup for it.

Thanks