我编写了一个连接到wamp服务器的应用程序(使用MySQl数据库,表用户中其行之一具有Username =“ pooriya”和Password =“ 123”) 此应用程序检查用户名“ pooriya”是否存在,然后输入Toast密码,如果不存在,则输入Toast“ no user”
当我在模拟器上运行此应用程序时,它应该吐司“ 123”,但是 显示为空的吐司。为什么呢 即使当我将用户更改为不存在的用户名(如“ poori”)时,也会显示空的Toast。为什么呢 数据库名称为“ note_test_2_db” 当我在浏览器中输入地址“ http://127.0.0.1:8080/mysite1/index.php”时,它显示“ no user”,那么我猜该php文件可以正常工作,而问题出在我的android代码中。 谢谢
package com.example.GetDataFromServer;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MyActivity extends Activity {
public static String res = "";
Button btn;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button);
new getdata("http://127.0.0.1:8080/mysite1/index.php", "pooriya").execute();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
}
});
}
}
package com.example.GetDataFromServer;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
/**
* Created with IntelliJ IDEA.
* User: Farid
* Date: 3/15/19
* Time: 4:09 PM
* To change this template use File | Settings | File Templates.
*/
public class getdata extends AsyncTask {
private String Link = "";
private String User = "";
public getdata(String link, String user) {
Link = link;
User = user;
}
@Override
protected String doInBackground(Object... objects) {
try {
String data = URLEncoder.encode("username", "UTF8") + "=" + URLEncoder.encode(User, "UTF8");
URL mylink = new URL(Link);
URLConnection connect = mylink.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr= new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
MyActivity.res = sb.toString();
} catch (Exception e) {
}
return ""; //To change body of implemented methods use File | Settings | File Templates.
}
}
$con=mysql_connect("localhost","root","");
mysql_select_db("note_test_2_db",$con);
$user=$_POST['username'];
$sqlQ="select * from users where Username='$user'";
$result= mysql_Query($sqlQ);
$row=mysql_fetch_array($result);
if($row[0]){
print $row[1];
}
else{
print "no user";
}
mysql_close($con);
答案 0 :(得分:1)
问题:看来您显示Toast
的代码不正确。
new getdata("http://127.0.0.1:8080/mysite1/index.php", "pooriya").execute();
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
}
});
执行第一行时,应用程序将启动AsyncTask
,该"123"
连接到您的服务器以获取响应("No User"
或btn
)。
如果在AsyncTask
完成之前单击按钮res
,此时""
的值为Toast
,这就是为什么总是空着{{ 1}}。
解决方案:您可以执行以下步骤
步骤1:由于getdata
是一个单独的类,因此您需要定义一个接口以传递数据("123"
或"No User"
或任何值)回到MyActivity
。
public interface OnDataListener {
void onData(String result);
}
第2步:修改getdata
类
public class getdata extends AsyncTask<Object, Void, String> {
private String Link = "";
private String User = "";
private WeakReference<OnDataListener> mListener;
public getdata(String link, String user, OnDataListener listener) {
Link = link;
User = user;
mListener = new WeakReference<>(listener);
}
@Override
protected String doInBackground(Object... objects) {
try {
String data = URLEncoder.encode("username", "UTF8") + "=" + URLEncoder.encode(User, "UTF8");
URL mylink = new URL(Link);
URLConnection connect = mylink.openConnection();
connect.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(connect.getOutputStream());
wr.write(data);
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connect.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
}
// This string will pass as param of onPostExecute method.
return sb.toString(); // Will return "123" or "No User" if there is no exception occurs.
} catch (Exception e) {
}
// If your app reach this line, it means there is an exception occurs, using a unique string for debugging.
// This string will pass as param of onPostExecute method
return "An exception has been caught!!!";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Pass the result back to MyActivity's onData method.
if (mListener != null && mListener.get() != null) {
mListener.get().onData(result);
}
}
}
步骤3:让MyActivity
实现OnDataListener
接口。
public class MyActivity extends AppCompatActivity implements OnDataListener {
public static String res = "";
Button btn;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.button);
new getdata("http://127.0.0.1:8080/mysite1/index.php", "pooriya", this).execute();
// TODO: Comment-out this code
// btn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Toast.makeText(getApplicationContext(), res, Toast.LENGTH_LONG).show();
// }
// });
}
@Override
public void onData(String result) {
// result is passed from the AsyncTask's onPostExecute method.
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
注意:由于在连接服务器时未使用任何加载指示器,因此需要等待几秒钟才能在屏幕上看到Toast。
答案 1 :(得分:0)