![](/img/trans.png)
[英]Spring: how to use Polymer push-manifest.json with HTTP2 Server Push in a Spring project
[英]How To Use Http Push With Python Server And Android Client?
我有一个由Python的BaseHTTPRequestHandler编写的简单Http服务器。 使用Java在Android中使用HttpClient编写的客户端。 我想将数据从python服务器推送到客户端java:因此,我尝试在服务器上进行一会儿True循环,并编写一些服务器代码:
def do_POST(self):
# receive POST data from client
# ...
# Then I want to keep alive this connection
self.send_response(200)
while True:
self.send_response(200)
time.sleep(5)
And HttpClient and MultipartEntity for java Client:
protected String doInBackground() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.104:8089");
MultipartEntity entity = new MultipartEntity();
entity.addPart("type", new StringBody("audio"));
byte []dataByte = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
entity.addPart("data", new ByteArrayBody(dataByte, "data"));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
// Please see This log will never print because blocked above
Log.d("MainActivity", "Server response");
}
我想在httpClient.execute()之后连续接收服务器的响应。 但是我被阻止在客户端的最后一个来源,因为服务器上的While True循环。
我猜httpClient.execute()正在等待http套接字关闭? 您能帮我解决这种情况吗? 我已经搜索了推送技术,似乎长时间拉动是另一种推送方式。
但是我认为推送是针对我的情况的更好的解决方案。
我认为问题出在服务器端代码中,其中:
while True:
self.send_response(200)
time.sleep(5)
do_Post
方法是一个处理程序,self.send_response(200)只是设置HTTP状态。 但实际上并没有通过电线发送任何东西。 而你永远都呆在那里。
我认为您应该彻底检查应用程序,服务器端和客户端的体系结构(我可能错了,但是不推荐使用DefaultHttpClient
[1]吗?
您是否考虑过使用websockets: https : //github.com/koush/android-websockets ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.