使用android5,6版本从python服务器获取空响应

2024-09-26 22:51:15 发布

您现在位置:Python中文网/ 问答频道 /正文

使用HttpURLConnection,我将post请求从android应用程序发送到python服务器,问题是它只在android 4版本中工作(得到正常的非空响应)(在android 5、6版本中不工作),最有趣的是服务器接受来自应用程序的请求,但应用程序得到空响应。我不明白为什么会这样。下面是我的代码。抱歉坏了英语:

HttpUrlConnection的代码:

URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setFixedLengthStreamingMode(param.getBytes().length);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
con.setRequestProperty("Authorization", "Bearer " + token);
PrintWriter out = new PrintWriter(con.getOutputStream());
out.print(param);
out.close();
String response = "";
Scanner inStream = new Scanner(con.getInputStream());
while (inStream.hasNextLine()) {
    response += (inStream.nextLine());
}

python服务器返回响应的函数:

def send_response(res):
    bytes__ = str.encode(str('\n\n\n' + '{' + res + '}'))
    self.wfile.write(bytes__)
    return

Tags: 代码版本服务器应用程序urlnewparamresponse

热门问题