我正在尝试连接Android应用程序与mysql数据库我花了超过14个小时来找到错误但我失败了我的Android应用程序没有连接到数据库我已经在端口80上安装了wamp服务器这是默认情况下。我在设备上检查我的Android应用程序。我的设备和开发机器连接在同一个路由器上,我使用ip route检查它
是192.168.8.101。我也试过这些网址
String login_url="http://10.0.2.2/receive.php";
String login_url="http://192.168.8.101/receive.php";
String login_url="http://127.0.0.1/receive.php";
所有这些网址都无效
这是我的安卓代码
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context ctx;
AlertDialog alertDialog;
BackgroundWorker(Context ctx){
ctx=ctx;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://10.0.2.2/receive.php";
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
@Override
protected void onPreExecute() {
alertDialog=new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
这是主要活动类
public class MainActivity extends AppCompatActivity {
EditText usernameEr,passwordEr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEr=(EditText) findViewById(R.id.editText);
passwordEr=(EditText) findViewById(R.id.editText2);
}
public void Login(View v){
String username=usernameEr.getText().toString();
String password=passwordEr.getText().toString();
String type="login";
BackgroundWorker BgWorker= new BackgroundWorker(this);
BgWorker.execute(type,username,password);
}
}
这是android清单代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.kidsklub.and">
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是php代码
<?php
include "connection.php";
$username = $_POST["user_name"];
$password = $_POST["password"];
$row=mysql_query("SELECT * FROM tayyab where username='$username' AND password='$password'") or die("query failed");
$row_count=mysql_num_rows($row);
if($row_count>=1){
echo "You have been logged in!";
}
else{
echo "You have been logged out!";
}
?>
答案 0 :(得分:0)
从result
方法调用返回doInBackground
变量。
你要回归null
。
在result
顶部声明class
变量。