【发布时间】:2014-05-21 10:30:24
【问题描述】:
请我已经设置了我的 wampserver 并通过 wifi 配置了我的 android 设备以正确访问数据库和服务器并且它运行良好。我想从 wampserver 中的数据库中选择数据并在 TextView 中显示数据,我正在使用 asyncTask 来执行这个似乎正在工作的操作。现在问题是结果。我已经尝试了几个选项来操纵它,但我仍然在 TextView 或 Toast 消息中收到“
”换行符。我可以弄清楚为什么我仍然得到这个回报。任何帮助将不胜感激。谢谢您
class SingmeIn extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try{
String username = u;
String password = p;
String link = "http://192.168.30.1/androidphp/loginget.php?username=" +username+"&password="+password;
URL url = new URL(link);
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI(link));
HttpResponse response = client.execute(request);
BufferedReader in = new BufferedReader
(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line="";
String[] data=null;
while((line=in.readLine())!=null){
data=line;
break;
}
in.close();
return data[0];
}
catch(Exception e)
{
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result){
//statusField.setText("Login Successful");
role.setText(result);
//System.out.println(result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
}
}
这是我的 PHP 代码:
<?php
$con=mysqli_connect("192.168.30.1","root"," ","searchmedb");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con,"SELECT UserType FROM accounts where UserName='$username' and UserPassword='$password'");
$row = mysqli_fetch_array($result);
$data = $row[0];
if($data){
echo $data;
}
mysqli_close($con);
?>
最初我在我的 while 循环中使用它,但它也不起作用:
while ((line = in.readLine()) != null) {
sb.append(line +"\n");
break;
}
in.close();
return sb.toString();
【问题讨论】:
-
你到底得到了什么?清楚
-
点击按钮后我得到的只是
-
你能确定你的 php 代码返回了什么吗?在使用 android 之前在浏览器中检查?
-
拜托,我已经检查过了,我的 php 代码正在运行
-
尝试一件事,在你的 PHP 代码中你得到 $_POST[] 变量,而在你的 android 中你已经完成了 GET 方法的编码,让它正确
标签: php android html mysql android-asynctask