gpt4 book ai didi

php - Android - 异常 : length=0; index=0

转载 作者:行者123 更新时间:2023-11-30 22:43:39 26 4
gpt4 key购买 nike

我正在开发 android 项目。用户应通过输入电子邮件和密码登录应用程序。登录表单使用 php 页面连接到 mySQL 数据库。我在屏幕顶部创建了一个 TextView 来显示从数据库中的用户表中检索到的用户名,还应该出现一个包含用户名的 toast ,上面写着“欢迎+用户名”,然后继续到下一页。或者如果有异常(exception),它会给出“异常(exception):+异常(exception)”问题是当我输入电子邮件和密码时, TextView 会显示用户名。但是 toast 给出了“异常:长度=0;索引=0”,所以它不会继续到下一页。

抱歉打字太久了。但我需要知道问题出在哪里。

这是我的安卓代码:


begin.java(主要 Activity )

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

最佳答案

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

您不应该显式调用 doInBackground(),它会在您调用 execute(param1, param2)在后台自动调用

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

关于php - Android - 异常 : length=0; index=0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30285472/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com