我正在编写一个 Android 应用程序,并且有一个登录 Activity ,用户必须在其中插入用户名和密码,因此我需要将我的应用程序与数据库连接起来以检查用户是否已注册。
我在 Altervista 上创建了一个 mysql 数据库,还创建了一个执行查询的 php 文件。 这是login.php的代码:
<?php
$UserName =$_POST['name'];
$Password =$_POST['pass'];
$hostname="localhost";
$user="myuser";
$pass="mypass";
$conn=mysql_connect($hostname,$user,$pass);
if(!$conn){
echo("Error");
}
$db=mysql_select_db('my_finditdatabase');
if(!$db)
{
echo "db non presente o mancata selezione";
}
$query="SELECT UserName,Password FROM Utente WHERE UserName='$UserName' AND Password='$Password';";
$ris=mysql_query($query);
$cont=0;
$riga=mysql_fetch_array($ris);
while($riga)
{
print(json_encode("Exist"));
$cont++;
$riga=mysql_fetch_array($ris);
}
if($cont==0){
print(json_encode("NoExist"));
echo mysql_error();
}
mysql_close();
?>
我需要连接到url,从java发送用户名和密码的值
$UserName =$_POST['name'];
$Password =$_POST['pass'];
然后从 php 文件接收打印“Exist”或“NoExist”。
print(json_encode("Exist"));
print(json_encode("NoExist"));
我已经尝试过来自互联网的代码,它们在许多已弃用的方法上给了我一些错误,例如:
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se;
se = new StringEntity(jsonObjSend.toString());
和其他...
我该怎么办?请帮助我,因为我真的需要它。谢谢
最佳答案
晚上好,杰伊, 首先让我们构建 JSON 并发送到服务器,在 Android APP 中创建 AsyncTask 或其他异步服务来进行 Web 连接。
然后按照以下示例进行操作:
HttpURLConnection urlConnection = null; // Will do the connection
BufferedReader reader; // Will receive the data from web
String strJsonOut; // JSON to send to server
Uri buildUri = Uri.parse("http://url.com.br").buildUpon().build(); // build the uri
//inside a try to prevent errors
try {
URL url = new URL(buildUri.toString().trim());
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(0);
urlConnection.setConnectTimeout(1500); // timeout of connection
urlConnection.setRequestProperty("Content-Type", "application/json"); // what format will you send
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true); // will receive
urlConnection.setDoOutput(true); // will send
urlConnection.connect();
Stream outputStream = urlConnection.getOutputStream();
OutputStreamWriter ow = new OutputStreamWriter(outputStream);
JSONObject objLogin = new JSONObject();
objLogin.put("pass",passVariable);
objLogin.put("name",nameVariable);
ow.write(objLogin.toString());
ow.close();
//information sent by server
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// response is empty, do someting
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String strJsonServer = buffer.toString();
objServerResponse = new JSONObject(strJsonServer);
boolean status = objServerResponse.getBoolean("status");
return status; // Your response, do what you want.
}catch (JSONException e, IOException er)
{
// error on the conversion or connection
}
在服务器上,只需创建一个包含状态键的数组即可。
$name = $_POST['name'];
$pass = $_POST['pass'];
// do your logical
$response = array();
if(true){
$response['status'] = 'true';
}
else{
$response['status']= 'false';
}
echo json_encode($response,JSON_UNESCAPED_UNICODE); // Second parameter to correct special characters to UTF-8
我现在无法测试这个解决方案,如果有一些错误,请检查我的语法是否正确并防止 catch 子句中可能出现的错误。
关于java - Android:将数据从java发送到php文件并有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37663429/