gpt4 book ai didi

android - 需要在异步任务中工作 'onPostExecute'

转载 作者:行者123 更新时间:2023-11-30 02:50:32 26 4
gpt4 key购买 nike

在 php 脚本的支持下,我有一段代码在 Async Task 中运行。我只想检查我的数据库中是否存在电子邮件 ID。我想显示 toast :“电子邮件已存在”(如果在数据库中)。否则另一个 toast :“注册成功”。我在下面给出的方法仅适用于其他条件。我将如何实现这一目标?请帮助我。

这是我的.java

class SummaryAsyncTask extends AsyncTask<Void, Boolean, String> {

private void postData(String fname,String lname,String email,String pass,String cpass,String mobile) {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx/registration.php");

try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);


nameValuePairs.add(new BasicNameValuePair("fname", fname));
nameValuePairs.add(new BasicNameValuePair("lname", lname));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
nameValuePairs.add(new BasicNameValuePair("cpass", cpass));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));


httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
httpEntity = response.getEntity();

InputStream is = httpEntity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8); // From here you can extract the data that you get from your php file..

StringBuilder builder = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}

is.close();

json = new JSONObject(builder.toString());// Here you are converting again the string into json object. So that you can get values with the help of keys that you send from the php side.

message = json.getString("message");

}

catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());

}

}
@Override
protected void onPostExecute(final String message) {
super.onPostExecute(message);
runOnUiThread(new Runnable() {
public void run() {
if (message!=null) {
Toast.makeText(Response.this,"Email Id already exist", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Response.this, "Thank You!You have registered successfully", Toast.LENGTH_LONG).show();
}
}
});
}
@Override
protected String doInBackground(Void... params) {
postData(first,last,eid,pword,conp,mob);
return null;
}


}

我的 .php 文件

<?php
$con=mysql_connect("localhost","root","");
$sel=mysql_select_db("xxx",$con);
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$email=$_POST['email'];
$pass=$_POST['pass'];
$cpass=$_POST['cpass'];
$mobile=$_POST['mobile'];

$query=mysql_query("SELECT * FROM user WHERE email='$email'");
if(mysql_num_rows($query)>0){
$response["message"] = "Email Id already exist.";
echo json_encode($response);
}

else{
mysql_query("insert into member(fname,lname,email,pass,cpass,mobile) values ('$fname','$lname', '$email', '$pass','$cpass','$mobile')", $con);
}
?>

最佳答案

因为您在onPostExecute 中的message 总是null 所以试试

protected String doInBackground(Void... params) {
return postData(first,last,eid,pword,conp,mob);
}

postData 方法应该返回 String。即将 postData 函数重写为

 private String postData(String fname,String lname,String email,String pass,String cpass,String mobile) {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://xxxx/registration.php");

try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);


nameValuePairs.add(new BasicNameValuePair("fname", fname));
nameValuePairs.add(new BasicNameValuePair("lname", lname));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("pass", pass));
nameValuePairs.add(new BasicNameValuePair("cpass", cpass));
nameValuePairs.add(new BasicNameValuePair("mobile", mobile));


httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(httppost);
httpEntity = response.getEntity();

InputStream is = httpEntity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8); // From here you can extract the data that you get from your php file..

StringBuilder builder = new StringBuilder();

String line = null;

while ((line = reader.readLine()) != null) {
builder.append(line + "\n");
}

is.close();

json = new JSONObject(builder.toString());// Here you are converting again the string into json object. So that you can get values with the help of keys that you send from the php side.

message = json.getString("message");

}

catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());

}

return message;

}

onPostExcute 方法一样

@Override
protected void onPostExecute(final String message) {
super.onPostExecute(message);
if (message!=null) {
Toast.makeText(Response.this,"Email Id already exist", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(Response.this, "Thank You!You have registered successfully", Toast.LENGTH_LONG).show();
}
}

关于android - 需要在异步任务中工作 'onPostExecute',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24258865/

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