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.

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

报告相同问题?

悬赏问题

  • ¥15 如何让子窗口鼠标滚动独立,不要传递消息给主窗口
  • ¥15 如何能达到用ping0.cc检测成这样?如图
  • ¥15 关于#DMA固件#的问题,请各位专家解答!
  • ¥15 matlab生成的x1图不趋于稳定,之后的图像是稳定的水平线
  • ¥15 请问华为OD岗位的内部职业发展通道都有哪些,以及各个级别晋升的要求
  • ¥20 微信小程序 canvas 问题
  • ¥15 系统 24h2 专业工作站版,浏览文件夹的图库,视频,图片之类的怎样删除?
  • ¥15 怎么把512还原为520格式
  • ¥15 MATLAB的动态模态分解出现错误,以CFX非定常模拟结果为快照
  • ¥15 求高通平台Softsim调试经验