1

This problems occur when I click on Login button on the android emulator, it appear on Username textbox.

Kindly help me to resolve it...your help is appreciate ~

java.lang.IllegalArgumentException: Illegal character in scheme at index 0:

android.os.NetworkOnMainThreadException

-- Update 7.9.2011

I post my code over here:

http://pastebin.com/EX0ArwaE --> Login.java

http://pastebin.com/WgGctGHN --> CustomHttpClient.java

4
  • Can you please provide the relevant code and the full stack traces? Commented Sep 7, 2011 at 3:46
  • Julian, do you means the java code? Commented Sep 7, 2011 at 4:09
  • Yes. Otherwise we can only guess what you're trying to do. Commented Sep 7, 2011 at 4:10
  • Julian, I already make update for my question and here is my php code : pastebin.com/7wYcYDys --> check.php Commented Sep 7, 2011 at 4:40

1 Answer 1

1

So first the Android stuff: You're getting this NetworkOnMainThreadException because you're trying to make a HTTP request on your main application thread which is the UI thread. You shouldn't do any blocking operations in this thread. Use an AsyncTask instead.

I'm not quite sure what's causing the IllegalArgumentException but I guess it's this line:

response = CustomHttpClient.executeHttpPost("http://127.0.0.1/es/check.php", postParameters);

You have probably changed the URL (localhost usually doesn't make sense on a phone). The scheme part is the http. Maybe you have something like " http://..." (note the leading space character) in the original code?

Short note on the PHP:

$sql = 'SELECT * from people WHERE username = "' .$_POST['un'] . '" and password = "' .md5($_POST['pw']) . '"';

That's what you call an SQL injection.

Update: Here's some example. Didn't test it, hopefully it works.

public class LoginLayout extends Activity {

    EditText un,pw;
    TextView error;
    Button ok;

    /** Called when the activity is first created. */

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

        un=(EditText)findViewById(R.id.et_un);
        pw=(EditText)findViewById(R.id.et_pw);
        ok=(Button)findViewById(R.id.btn_login);
        error=(TextView)findViewById(R.id.tv_error);

        ok.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new LoginTask().execute(un.getText().toString(), pw.getText().toString());
            }

        });
    }

    private class LoginTask extends AsyncTask<String, Void, Object> {

        protected Object doInBackground(String... params) {
            ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("username", params[0]));
            postParameters.add(new BasicNameValuePair("password", params[1]));

            String response = null;

            try {
                response = CustomHttpClient.executeHttpPost("http://127.0.0.1/es/check.php", postParameters);
                String res = response.toString();
                res = res.replaceAll("\\s+","");
                return res;
            } catch (Exception e) {
                return e;
            }
        }

        protected void onPostExecute(Object result) {
            if (result instanceof String) {
                if (result.equals("1")) {
                    error.setText("Correct Username or Password");
                } else {
                    error.setText("Sorry!! Wrong Username or Password Entered");
                }
            } else if (result instanceof Exception) {
                un.setText(result.toString());
            }
        }
    }

}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Julian...I try it already but its gt error at the last paragraph: else if (result instanceof Exception) { un.setText(e.toString()); } at the (e.toString());
thanks again Julian, the java code I think don't have any problem. The problem now occur is on my check.php url.
One problem I see (besides the SQL Injection which you really should fix, use mysql_real_escape_string()) is that you're sending username and password but in the PHP script you try to access un and pw:
izzit now my coding should be like that : $sql = 'SELECT * from people WHERE username = "' . mysql_real_escape_string($_POST['username']) . '" and password = "' . $_POST['password'] . '"';
@RYS Yeah, but I guess you still want to hash the password. ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.