Android emulator java.lang.IllegalArgumentException:索引0处的方案中的非法字符:和android.os.NetworkOnMainThreadException

Android emulator java.lang.IllegalArgumentException:索引0处的方案中的非法字符:和android.os.NetworkOnMainThreadException,android-emulator,Android Emulator,当我点击android模拟器上的登录按钮时,就会出现这个问题,它出现在用户名文本框中 请帮我解决…谢谢你的帮助~ java.lang.IllegalArgumentException:索引0处的方案中存在非法字符: android.os.NetworkOnMainThreadException -- 更新7.9.2011 我把我的代码贴在这里: -->Login.java -->CustomHttpClient.java首先是Android的东西:您得到了这个NetworkOnMainThrea

当我点击android模拟器上的登录按钮时,就会出现这个问题,它出现在用户名文本框中

请帮我解决…谢谢你的帮助~

java.lang.IllegalArgumentException:索引0处的方案中存在非法字符:

android.os.NetworkOnMainThreadException

-- 更新7.9.2011

我把我的代码贴在这里:

-->Login.java


-->CustomHttpClient.java

首先是Android的东西:您得到了这个
NetworkOnMainThreadException
,因为您试图在主应用程序线程(UI线程)上发出HTTP请求。您不应该在此线程中执行任何阻塞操作。改用一个字母

我不太确定是什么导致了
IllegalArgumentException
,但我猜是这一行:

response = CustomHttpClient.executeHttpPost("http://127.0.0.1/es/check.php", postParameters);
您可能已经更改了URL(localhost在电话上通常没有意义)。方案部分是
http
。也许你有类似于
”的东西http://...“
(注意前导空格字符)是否在原始代码中

关于PHP的简短说明:

$sql = 'SELECT * from people WHERE username = "' .$_POST['un'] . '" and password = "' .md5($_POST['pw']) . '"';
这就是你所说的一个

更新:以下是一些示例。没有测试它,希望它能工作

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

}
公共类LoginLayout扩展活动{
编辑文本un,pw;
文本视图错误;
按钮ok;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
un=(编辑文本)findViewById(R.id.et_un);
pw=(编辑文本)findViewById(R.id.et_pw);
ok=(按钮)findViewById(R.id.btn\u登录);
错误=(TextView)findViewById(R.id.tv_错误);
ok.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新的LoginTask().execute(un.getText().toString(),pw.getText().toString());
}
});
}
私有类LoginTask扩展异步任务{
受保护对象doInBackground(字符串…参数){
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,参数[0]);
添加(新的BasicNameValuePair(“密码”,参数[1]);
字符串响应=null;
试一试{
响应=CustomHttpClient.executeHttpPost(“http://127.0.0.1/es/check.php“,后参数);
String res=response.toString();
res=res.replaceAll(“\\s+”,”);
返回res;
}捕获(例外e){
返回e;
}
}
受保护的void onPostExecute(对象结果){
if(字符串的结果实例){
如果(结果等于(“1”)){
错误.setText(“正确的用户名或密码”);
}否则{
error.setText(“对不起!!输入了错误的用户名或密码”);
}
}else if(异常的结果实例){
un.setText(result.toString());
}
}
}
}

首先是Android的东西:你得到了这个
NetworkOnMainThreadException
,因为你试图在你的主应用程序线程(UI线程)上发出HTTP请求。您不应该在此线程中执行任何阻塞操作。改用一个字母

我不太确定是什么导致了
IllegalArgumentException
,但我猜是这一行:

response = CustomHttpClient.executeHttpPost("http://127.0.0.1/es/check.php", postParameters);
您可能已经更改了URL(localhost在电话上通常没有意义)。方案部分是
http
。也许你有类似于
”的东西http://...“
(注意前导空格字符)是否在原始代码中

关于PHP的简短说明:

$sql = 'SELECT * from people WHERE username = "' .$_POST['un'] . '" and password = "' .md5($_POST['pw']) . '"';
这就是你所说的一个

更新:以下是一些示例。没有测试它,希望它能工作

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

}
公共类LoginLayout扩展活动{
编辑文本un,pw;
文本视图错误;
按钮ok;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
un=(编辑文本)findViewById(R.id.et_un);
pw=(编辑文本)findViewById(R.id.et_pw);
ok=(按钮)findViewById(R.id.btn\u登录);
错误=(TextView)findViewById(R.id.tv_错误);
ok.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
新的LoginTask().execute(un.getText().toString(),pw.getText().toString());
}
});
}
私有类LoginTask扩展异步任务{
受保护对象doInBackground(字符串…参数){
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“用户名”,参数[0]);
添加(新的BasicNameValuePair(“密码”,参数[1]);
字符串响应=null;
试一试{
响应=CustomHttpClient.executeHttpPost(“http://127.0.0.1/es/check.php“,后参数);
String res=response.toString();
res=res.replaceAll(“\\s+”,”);
返回res;
}捕获(例外e){
返回e;
}
}
受保护的void onPostExecute(对象结果){
if(字符串的结果实例){
如果(结果等于(“1”)){
错误.setText(“正确的用户名或密码”);
}否则{
error.setText(“对不起!!输入了错误的用户名或密码”);
}
}else if(异常的结果实例){
un.setText(result.toString());
}
}
}
}

您能提供相关的代码和密码吗