Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将数据从Python发送到Android应用程序_Php_Android_Python_Json - Fatal编程技术网

Php 如何将数据从Python发送到Android应用程序

Php 如何将数据从Python发送到Android应用程序,php,android,python,json,Php,Android,Python,Json,我正在努力从Python(主机ip地址为192.168.2.20/Fetch/Fetch.py)向Android发送JSON数据。我曾经在php中这样做,非常简单,只需编写以下代码: echo json_encode($thedata); 但Python似乎很难做到这一点,我不知道如何做到这一点,web似乎根本没有帮助,因为大多数教程都建议使用框架。因此,我如何在我的Android应用程序中包含python文件并获取JSON,而不使用FLASK。如果您正在做任何有用的事情,除了将python作

我正在努力从Python(主机ip地址为192.168.2.20/Fetch/Fetch.py)向Android发送JSON数据。我曾经在php中这样做,非常简单,只需编写以下代码:

echo json_encode($thedata);

但Python似乎很难做到这一点,我不知道如何做到这一点,web似乎根本没有帮助,因为大多数教程都建议使用框架。因此,我如何在我的Android应用程序中包含python文件并获取JSON,而不使用FLASK。

如果您正在做任何有用的事情,除了将python作为Android应用程序后端,您应该开始寻找标准python框架来处理HTTP服务器操作,如django或FLASK

这就是说,有一个小存根,我用它作为我传出请求的测试服务器,它应该能回答您的问题。您可以通过修改来设置任何状态代码、标题或响应正文:

#!/usr/bin/env python
# Reflects the requests with dummy responses from HTTP methods GET, POST, PUT, and DELETE
# Written by Tushar Dwivedi (2017)

import json
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from optparse import OptionParser

class RequestHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        request_path = self.path

        print("\n----- Request Start ----->\n")
        print("request_path :", request_path)
        print("self.headers :", self.headers)
        print("<----- Request End -----\n")

        self.send_response(200)
        self.send_header("Set-Cookie", "foo=bar")
        self.end_headers()
        self.wfile.write(json.dumps({'hello': 'world', 'received': 'ok'}))

    def do_POST(self):
        request_path = self.path

        # print("\n----- Request Start ----->\n")
        print("request_path : %s", request_path)

        request_headers = self.headers
        content_length = request_headers.getheaders('content-length')
        length = int(content_length[0]) if content_length else 0

        # print("length :", length)

        print("request_headers : %s" % request_headers)
        print("content : %s" % self.rfile.read(length))
        # print("<----- Request End -----\n")

        self.send_response(200)
        self.send_header("Set-Cookie", "foo=bar")
        self.end_headers()
        self.wfile.write(json.dumps({'hello': 'world', 'received': 'ok'}))

    do_PUT = do_POST
    do_DELETE = do_GET


def main():
    port = 8082
    print('Listening on localhost:%s' % port)
    server = HTTPServer(('', port), RequestHandler)
    server.serve_forever()


if __name__ == "__main__":
    parser = OptionParser()
    parser.usage = ("Creates an http-server that will echo out any GET or POST parameters, and respond with dummy data\n"
                    "Run:\n\n")
    (options, args) = parser.parse_args()

    main()
#/usr/bin/env python
#使用来自HTTP方法GET、POST、PUT和DELETE的虚拟响应反映请求
#由Tushar Dwivedi撰写(2017年)
导入json
从BaseHTTPServer导入HTTPServer,BaseHTTPRequestHandler
从optpasse导入OptionParser
类RequestHandler(BaseHTTPRequestHandler):
def do_获得(自我):
请求路径=self.path
打印(“\n-----请求开始------>\n”)
打印(“请求路径:”,请求路径)
打印(“self.headers:”,self.headers)
打印(“\n”)
打印(“请求路径:%s”,请求路径)
请求头=self.headers
content\u length=request\u headers.getheaders('content-length')
如果内容长度为0,则长度=int(内容长度[0])
#打印(“长度:”,长度)
打印(“请求\u头:%s”%request\u头)
打印(“内容:%s”%self.rfile.read(长度))

#print(@ShakibKarami)您提到的问题涉及使用python向服务器发送HTTP请求。这是关于从服务器端接收HTTP请求并发送响应的问题。@Tushar是的,谢谢,我删除了这个问题