Android应用程序没有连接到mysql数据库
问题描述:
我想连接android应用程序与MySQL数据库我花了超过14个小时找到错误,但我失败了,我的android应用程序没有连接到数据库我已经安装了wamp服务器在默认端口80上。我正在检查设备上的Android应用程序。我的设备和开发机连接在同一个路由器上,我已经使用IP路由检查它Android应用程序没有连接到mysql数据库
这是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";
所有这些网址无法正常工作
这是我的Android代码
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!";
}
?>
答
从doInBackground
方法返回result
varible呼叫。 您正在返回null
。
声明result
变量在class
之上。
你能指出它吗? –
不工作!我已经在顶部声明,但仍然没有响应 –
public class BackgroundWorker extends AsyncTask String result = new String(); 上下文ctx; AlertDialog alertDialog; ' –