Android - 例外:长度= 0;索引= 0

时间:2015-05-17 09:36:12

标签: php android mysql

我正在开发android项目。用户应通过键入电子邮件和密码登录应用程序。登录表单使用php页面连接到mySQL数据库。 我在屏幕顶部创建了一个文本视图,以显示从数据库中的users表中检索到的用户名,还应该出现一个toast,其中包含的用户名为" welcome + username"然后继续下一页。或者如果有异常则给出"异常:+异常" 问题是当我输入电子邮件和密码时,文本视图会显示用户名。但吐司给出了#例外:长度= 0;索引= 0"所以它不会继续下一页。

很抱歉长时间打字。但我需要知道问题是什么。

这是我的android代码:

begin.java(主要活动)

btnn.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                check_mail = nam2.getText().toString();
                check_pass = pas2.getText().toString();

                if ((check_mail.length()==0) || (check_pass.length()==0)) {
                    enterMessage();
                }
                else{
                    if(check_mail.matches("[a-zA-Z0-9._]+@[a-z]+\\.+[com]+")){

                try{

                    new SigninActivity2(context,role,1).execute(check_mail,check_pass);
                    String ss = new SigninActivity2(context,role,1).doInBackground();
                        if((ss!=null) && (ss!=("</ br>")) && (ss!="")){
                            if (ss.contains("Exception")){
                                Toast.makeText(getApplicationContext(), ss, Toast.LENGTH_LONG).show();
                            }else{
                            Toast.makeText(getApplicationContext(), "welcome "+check_mail.substring(0,4), Toast.LENGTH_LONG).show();
                            Intent myIntent = new Intent(context, start2.class);
                            startActivity(myIntent);
                            }
                        }
                            else
                                Toast.makeText(getApplicationContext(), "wrong email or password", Toast.LENGTH_LONG).show();



             }catch (Exception e){
                    Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();

             }}else{
                 emailMessage();
             }}
             }
           });

这是异步任务:SigninActivity2.java

public class SigninActivity2 extends AsyncTask<String,Void,String>{

    private TextView roleField; 
    private Context context; 
    private int byGetOrPost = 0;
    StringBuffer sb;
    HttpResponse response;
    //flag 0 means get and 1 means post.(By default it is get.) 
    public SigninActivity2(Context context, TextView roleField,int flag) {
        this.context = context;
        this.roleField = roleField;
        byGetOrPost = flag; 
        } 
    protected void onPreExecute(){ 

    }

    @Override
    protected String doInBackground(String... arg0) {
        if(byGetOrPost == 1){
            //means by Get Method 
        /*  try{ 
                String email = (String)arg0[0];
                String password = (String)arg0[1];
            String link = "http://192.168.1.50/applogin.php?email="+email+"&password="+password;
            URL url = new URL(link);
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI(link));
            response = client.execute(request);
            BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
            sb = new StringBuffer("");
            String line="";
            while ((line = in.readLine()) != null) {
                sb.append(line);
                break;
                } 
            in.close(); 
            return sb.toString();

            }
            catch(Exception e){
                    return new String("Exception: " + e.getMessage());

                }
            }
        else{}*/
                try{
                    String email = (String)arg0[0]; 
                    String password = (String)arg0[1];
                    String link="http://192.168.1.20/applogin2.php";
                    String data = URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "UTF-8");
                    data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                    URL url = new URL(link);
                    URLConnection conn = url.openConnection(); 
                    conn.setDoOutput(true); 
                    OutputStreamWriter wr = new OutputStreamWriter (conn.getOutputStream());
                    wr.write( data );
                    wr.flush();
                    BufferedReader reader = new BufferedReader (new InputStreamReader(conn.getInputStream())); 
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    // Read Server Response
                    while((line = reader.readLine()) != null) {
                        sb.append(line);
                        break; 
                        } 
                    return sb.toString();
                    }
                catch(Exception e){
                    return new String("Exception: " + e.getMessage());
                    } 

        }
        return sb.toString();
            }
    @Override
    protected void onPostExecute(String result){
        this.roleField.setText(result); 
        }
        }

最后这个php代码:applogin2.php

<?php

/**
 * @author 
 * @copyright 2015
 */
$con=mysqli_connect("localhost","root","","school"); 
if (mysqli_connect_errno($con))
 {
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
 }
 $email = $_POST['email']; 
 $password = $_POST['password'];
 $result = mysqli_query($con,"SELECT name FROM teacher where email='$email' and password='$password'");
 $row = mysqli_fetch_array($result); 
 $data = $row[0]; 
 if($data){ echo $data; } mysqli_close($con);



?>


![logcat][1]


enter code here

1 个答案:

答案 0 :(得分:0)

删除该行: String ss = new SigninActivity2(context,role,1).doInBackground();

当您致电doInBackground()

时,您不应该明确致电execute(param1, param2),它会在后台自动调用

另外,将所有与字符串ss相关的代码移到SigninActivity2的onPostExecute()内。