首页 文章

Android:将数据从java发送到php文件并有响应

提问于
浏览
1

我正在编写一个Android应用程序,我有一个登录活动,用户必须插入一个UserName和密码,所以我需要将我的应用程序与我的数据库连接,以检查用户是否注册 .

我在Altervista上创建了一个mysql数据库,还有一个执行查询的php文件 . 这是login.php的代码:

<?PHP

$ UserName = $ _ POST ['name'];
$ Password = $ _ POST ['pass'];

$主机= “localhost” 的;
$用户= “为myuser”;
$传= “将mypass”;

$康恩=的mysql_connect($主机名,用户$,$通);
如果(!$康恩){
回波( “错误”);
}

$分贝= mysql_select_db( 'my_finditdatabase');
如果(!$ DB)
{
echo“db non presente o mancata selezione”;
}

$ query =“SELECT UserName,Password FROM Utente WHERE UserName ='$ UserName'AND Password ='$ Password';”;

$ RIS =的mysql_query($查询);
$ CONT = 0;
$里加= mysql_fetch_array($ RIS);
而($里加)
{
打印(json_encode( “存在”));
$ cont;
$里加= mysql_fetch_array($ RIS);
}

如果($续== 0){
打印(json_encode( “NoExist”));
echo mysql_error();
}
mysql_close();
?>

我需要连接到url,从java发送用户名和密码的值
$ UserName = $ _ POST ['name'];
$ Password = $ _ POST ['pass'];
然后从php文件接收print 'Exist'或'NoExist' .
打印(json_encode( “存在”));

打印(json_encode( “NoExist”));
我已经尝试过来自Internet的代码,他们给了我一些关于许多弃用方法的错误,例如:DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPostRequest = new HttpPost(URL);
StringEntity se; se = new StringEntity(jsonObjSend.toString());
和别的...

我能怎么做 ?请帮助我,因为我真的需要它 . 谢谢

1 回答

  • 0

    晚安Jay,首先让我们构建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子句上的可能错误 .

相关问题