开发者

java.lang.IllegalArgumentException: Illegal character in scheme at index 0: AND android.os.NetworkOnMainThreadException

This problems oc开发者_JAVA百科cur 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


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());
            }
        }
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜