public class Sign_In extends Activity {
EditText et_user , et_pass;
Button login ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
et_user = (EditText) findViewById(R.id.et_user_login);
et_pass = (EditText) findViewById(R.id.et_pass_login);
login = (Button) findViewById(R.id.btlogin);
login.setOnClickListener(new View.OnClickListener() {
public void onClick(View V) {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/koora/checkPerson.php?user="+et_user.getText().toString()+
"pass="+et_pass.getText().toString());
try
{
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
InputStream is = httpEntity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
Toast.makeText(getApplicationContext(), sb.toString(), Toast.LENGTH_LONG).show();
reader.close();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
Log.d("Http Response:", response.toString());
}
catch(
ClientProtocolException e
)
{
e.printStackTrace();
}
catch(
IOException e
)
{
e.printStackTrace();
}
}
});
}
}
这是php代码:
<?php
$username=$_REQUEST["user"];
$password=$_REQUEST["pass"];
include 'config.php';
$response=array();
function connect(){
return mysqli_connect(HOST,USER,PASS,DATABASE);
}
$query=" select username , password from person where username = ".$username." and password = ".$password." ";
$query_res=mysqli_query(connect(),$query);
$num_results=mysqli_num_rows($query_res);
if($num_results>0){
$response["message"]="login successful";
echo json_encode($response);
}else{
$response["message"]="login unsuccessful";
echo json_encode($response);
}
?>
配置文件:
<?php
define("HOST","localhost");
define("DATABASE","final_project");
define("USER","root");
define("PASS","");
?>
我当然正在使用wamp服务器,我将internet权限放入manifest.xml文件
和错误:
android.os.NetworkOnMainThreadException 在android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145) 在libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84) 在libcore.io.IoBridge.connectErrno(IoBridge.java:127) 在libcore.io.IoBridge.connect(IoBridge.java:112) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459) 在java.net.Socket.connect(Socket.java:843) 在org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144) 在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164) 在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360) 在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555) 在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487) 在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465) 在com.koora.com.koora.Sign_In $ 1.onClick(Sign_In.java:43) 在android.view.View.performClick(View.java:4438) 在android.view.View $ PerformClick.run(View.java:18422) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在dalvik.system.NativeStart.main(本地方法)