目前我正在进行登录和注册教程。我完全按照教程,但失败了。我的Php脚本很好,它们连接到我的localhost服务器。但是,当我在我的应用程序上运行时,它不起作用而根本没有响应,请帮我查看是否有任何错误。我的manifest.xml已经编写了代码权限internet。我的应用程序无法链接并且没有响应。
这是我的链接类BackgroundTask.class
package com.example.lam.loginregistertest2;
import android.content.Context;
import android.os.AsyncTask;
import android.widget.Toast;
import org.apache.http.HttpConnection;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
/**
* Created by LAM on 3/8/2015.
*/
public class BackgroundTask extends AsyncTask<String,Void,String> {
Context ctx;
BackgroundTask(Context ctx) {
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://192.168.43.225/webapp/register.php";
String login_url = "http://10.0.2.2/webapp.login.php";
String method = params[0];
if (method.equals("register"))
{
String name = params[1];
String age = params[2];
String username = params[3];
String password = params[4];
try {
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("name","UTF-8") +"="+URLEncoder.encode(name,"UTF-8")+"&"+
URLEncoder.encode("age","UTF-8") +"="+URLEncoder.encode(age,"UTF-8")+"&"+
URLEncoder.encode("username","UTF-8") +"="+URLEncoder.encode(username,"UTF-8")+"&"+
URLEncoder.encode("password","UTF-8") +"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
register.php
<?php
require "init.php";
$name = isset ($_POST["name"]) ? $_POST["name"] : "";
$age = isset ($_POST["age"]) ? $_POST["age"] : "";
$username = isset ($_POST["username"]) ? $_POST["username"] : "";
$password = isset ($_POST["password"]) ? $_POST["password"] : "";
$sql_query = "insert into user values('$name','$age','$username','$password');";
if (mysqli_query($con,$sql_query)){
//echo "<h3>Data Insertion Success...</h3>";
}else{
//echo "Data insertion error..".mysqli_error($con);
}
?>
的init.php
<?php
$db_name = "test";
$mysql_user = "root";
$mysql_pass = "cheehong";
$server_name = "localhost";
$con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name);
if(!$con){
//echo "Connection Error...".mysql_connect_error();
}else{
//echo "<h3>Database Connection Successful...</h3>";
}
?>
的login.php
<?php
require "init.php";
$username = "ivan";
$password = "12";
$sql_query = "select name from user where username like '$username' and password like '$password';";
$result = mysqli_query ($con,$sql_query);
if(mysqli_num_rows($result)>0){
$row = mysqli_fetch_assoc($result);
$name = $row["name"];
echo "<h3>Hello welcome".$name."</h3>";
}else{
echo "No info is available.";
}
?>
Register.class
package com.example.lam.loginregistertest2;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
public class Register extends Activity {
EditText ET_NAME, ET_USER_NAME,ET_USER_PASS,ET_AGE;
String name,age, username,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
ET_NAME = (EditText)findViewById(R.id.name);
ET_AGE = (EditText)findViewById(R.id.age);
ET_USER_NAME = (EditText)findViewById(R.id.new_user_name);
ET_USER_PASS = (EditText)findViewById(R.id.new_user_pass);
}
public void userReg(View view)
{
name = ET_NAME.getText().toString();
age = ET_AGE.getText().toString();
username = ET_USER_NAME.getText().toString();
password = ET_USER_PASS.getText().toString();
String method = "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,name,age,username,password);
finish();
}
}
答案 0 :(得分:0)
检查您正在使用的网址
String reg_url = "http://192.168.43.225/webapp/register.php";
String login_url = "http://10.0.2.2/webapp.login.php";
两者都不同
答案 1 :(得分:0)
您确定您的服务器和设备是否位于同一网络中?如果不确定设备可以访问服务器。