douyinlai2169 2015-04-25 22:00
浏览 57
已采纳

OKHTTP - 发布用户输入数据,检索JSON响应以确定成功

I've been thinking in how to do this. I've read trough the OKHTTP documentation, I have an idea on how to handle this but I'm not entirely sure.

I'll have the user submit three fields ( Username, Password, Database ). I'll have a PHP file uploaded to my host which accepts post data. ( It's pure PHP, so no html. It just accepts data posted to the page. )

Once the values have been submitted, i'll perform a check. Credentials found? Return/echo JSON array ( Success/ Failure ). I could check if the array key " success " equals either 1 || 0. If 1, show a toast that user has been logged in successfully.

Now, as for the question. Is my logic correct? If not, how would you do it? Do you have any tips for me? Any examples?

  • 写回答

1条回答 默认 最新

  • douyi0902 2015-04-26 09:48
    关注

    To send the "authorization" in HTTP requests you have to set the header to 403.

    header('HTTP/1.0 403 Forbidden');
    
    echo 'You are forbidden!';
    

    But when I send JSON via a PHP page, I am using a 'generator' to make it easy, like Simple JSON for PHP.

    include('includes/json.php');
    
    $json = new json();
    
    $json->add('status', '403');
    $json->add('message', 'YOU ARE FORBIDDEN!');
    
    $json->send();
    

    UPDATE : Headers if you want to send "home made" JSON :

    header('Cache-Control: no-cache, must-revalidate');
    header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
    header('Content-type: application/json');
    

    EDIT :

    There is also the Slim Framework, I didn't try it, but it seems very efficient! Simple JSON for PHP is as the name says, just to create a JSON & send it, it do not use make use of routes at all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?