如何使用PHP连接android studio中的mysql数据库? [英] How to connect to mysql database in android studio using PHP?
本文介绍了如何使用PHP连接android studio中的mysql数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用php和php conect连接到android工作室上的mysql,但问题是android代码。
我是什么尝试过:
I am trying to connect to mysql on android studio with php and php conect right but is the android code that is the problem.
What I have tried:
public class BackgroundWorker extends AsyncTask<String, Void, String> {
Context context;
ProgressDialog alertDialog;
public BackgroundWorker(Context context){
this.context = context;
}
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://127.0.0.0/login.php";
if(type.equals("login")){
try {
String user_name = params[1];
String user_pass = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
OutputStream outputStream = httpURLConnection.getOutputStream(); //When it get to this line of code it jumps out to the exception
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("user_pass", "UTF-8")+"="+URLEncoder.encode(user_name, "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) { //It jups to this exception and give an error
e.printStackTrace();
}
}
return null;
}
推荐答案
在这种情况下提供完整的异常消息。
但您的问题很可能来自指定无效的网址:
In such cases provide the full exception message.
But your problem is very probably sourced by specifying an invalid URL:
String login_url = "http://127.0.0.0/login.php";
It必须是127.0.0。 1 。
It must be 127.0.0.1.
这篇关于如何使用PHP连接android studio中的mysql数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文