【问题标题】:Android:Send data from java to a php file and have a responseAndroid:将数据从java发送到php文件并有响应
【发布时间】:2023-04-06 14:57:01
【问题描述】:

我正在编写一个 android 应用程序,并且我有一个登录活动,用户必须在其中插入用户名和密码,因此我需要将我的应用程序与我的数据库连接以检查用户是否已注册。

我在 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"));

我已经尝试过来自 Internet 的代码,它们在许多已弃用的方法上给了我一些错误,例如: DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se; se = new StringEntity(jsonObjSend.toString());
还有其他...

我该怎么办?请帮助我,因为我真的需要它。谢谢

【问题讨论】:

    标签: java php android json database


    【解决方案1】:

    晚上好,杰伊, 首先让我们构建 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 子句中可能出现的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-11
      相关资源
      最近更新 更多