我只想知道我的申请中遗漏了什么
我的Android应用程序将用户名和密码作为HTTP POST发送,然后PHP文件用数据库中该用户名的值进行响应
问题是当我输入用户名和密码时,Toast中没有返回任何内容,甚至都不显示
主要活动
package com.example.postapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView tv;
EditText u,p;
Button login;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
u = (EditText) findViewById(R.id.username);
p = (EditText) findViewById(R.id.password);
Toast.makeText(getApplicationContext(), u.getText().toString(), Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), p.getText().toString(), Toast.LENGTH_LONG).show();
new MyAsyncTask().execute(u.getText().toString(),p.getText().toString());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class MyAsyncTask extends AsyncTask<String, Integer, Boolean>{
String host;
@Override
protected Boolean doInBackground(String... params) {
// TODO Auto-generated method stub
Boolean a= postData(params[0],params[1]);
return a;
}
protected void onPostExecute(Boolean localres){
tv = (TextView) findViewById(R.id.tv);
if (localres){
tv.setText("A Correct Username and Password");
}else{
tv.setText("Incorrect Username or Password");
}
// Toast.makeText(getApplicationContext(), result.toString(), Toast.LENGTH_SHORT).show();
// Toast.makeText(getApplicationContext(), "command sent", Toast.LENGTH_SHORT).show();
if(host!=null)
{
// Toast.makeText(getApplicationContext(), host.toString(), Toast.LENGTH_LONG).show();
}
}
protected void onProgressUpdate(Integer... progress){
//pb.setProgress(progress[0]);
//Toast.makeText(getApplicationContext(), "Done", Toast.LENGTH_LONG).show();
}
public Boolean postData(String a,String b) {
// ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
// postParameters.add(new BasicNameValuePair("username", a));
// postParameters.add(new BasicNameValuePair("password", b));
HttpURLConnection connection;
OutputStreamWriter request = null;
URL url = null;
String response = null;
String parameters = "username="+a+"&password="+b;
try
{
url = new URL("http://"+"192.168.1.3"+"/new/check2.php");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");
request = new OutputStreamWriter(connection.getOutputStream());
request.write(parameters);
request.flush();
request.close();
String line = "";
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
BufferedReader reader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
// Response from server after login process will be stored in response variable.
response = sb.toString();
// You can perform UI operations here
Toast.makeText(getApplicationContext(),"Message from Server: \n"+ response, 0).show();
isr.close();
reader.close();
}
catch(Exception e)
{
e.printStackTrace();
}
return true;
}
}
}
菲律宾比索
<?php
$con = mysqli_connect("localhost", "root", "123", "pet_home");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysql_query("select * from users where username='$username' and password='$password'")or die (mysql_error());
$count = mysql_num_rows($result);
$row = mysql_fetch_array($result);
if ($count > 0) {
echo $row['filter_st'];
echo "<br>";
echo $row['heat_st'];
echo "<br>";
echo $row['led_st'];
} else {
echo 0;
}
mysqli_close($con);
Logcat出现异常
12-13 20:57:45.534: W/System.err(14768): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-13 20:57:45.534: W/System.err(14768): at android.os.Handler.<init>(Handler.java:197)
12-13 20:57:45.534: W/System.err(14768): at android.os.Handler.<init>(Handler.java:111)
12-13 20:57:45.538: W/System.err(14768): at android.widget.Toast$TN.<init>(Toast.java:324)
12-13 20:57:45.538: W/System.err(14768): at android.widget.Toast.<init>(Toast.java:91)
12-13 20:57:45.538: W/System.err(14768): at android.widget.Toast.makeText(Toast.java:238)
12-13 20:57:45.538: W/System.err(14768): at com.example.postapp.MainActivity$MyAsyncTask.postData(MainActivity.java:136)
12-13 20:57:45.538: W/System.err(14768): at com.example.postapp.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:76)
12-13 20:57:45.538: W/System.err(14768): at com.example.postapp.MainActivity$MyAsyncTask.doInBackground(MainActivity.java:1)
12-13 20:57:45.538: W/System.err(14768): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-13 20:57:45.538: W/System.err(14768): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
12-13 20:57:45.538: W/System.err(14768): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-13 20:57:45.538: W/System.err(14768): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
12-13 20:57:45.538: W/System.err(14768): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
12-13 20:57:45.538: W/System.err(14768): at java.lang.Thread.run(Thread.java:841)
最佳答案
Toast.makeText(getApplicationContext(),"Message from Server: \n"+ response, 0).show();
你有
Boolean a= postData(params[0],params[1]); // in doInbackground
在
public Boolean postData(String a,String b)
中显示toast,这是错误的您正在后台线程中更新ui,这是不可能的。更新ui线程上的ui。
在
doInbackground
中返回结果。doInbackground
计算的结果是param toonPostExecute
。因此根据结果,您可以相应地更新ui-ie-display-toast。或使用
runOnUiThread
runOnUiThread(new Runnable(){
public void run() {
Toast.makeText(getApplicationContext(),"Message from Server: \n"+ response, 0).show();
}
});