gpt4 book ai didi

android - 如何从android接收和返回webapp2中的字符串?

转载 作者:太空宇宙 更新时间:2023-11-04 04:40:43 25 4
gpt4 key购买 nike

我正在努力将字符串接收到我的 webapp2 服务器中。android 设备应该向服务器发送一个字符串 name 然后服务器返回给手机 "your name is: "+ name.

服务器只返回 android"your name is: " 因为 name 字符串在服务器端似乎为空。我不确定问题是在 Android 端还是在服务器端。

我的代码如下:

Android.java

Thread thread = new Thread(new Runnable() {

@Override
public void run() {
try {
String name = "Jay";
String serverURL = "http://myapp.appspot.com";
URL url = new URL(serverURL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
try{
httpURLConnection.setReadTimeout(15000);
httpURLConnection.setConnectTimeout(15000);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);

OutputStream outputStream = new BufferedOutputStream(httpURLConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode(name,"UTF-8");
writer.write(post_data);
writer.flush();
writer.close();
outputStream.close();

InputStream inputStream = new BufferedInputStream(httpURLConnection.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = reader.readLine())!= null)
result += line;
reader.close();
inputStream.close();

} catch (MalformedURLException e) {
e.printStackTrace();
} finally {
httpURLConnection.disconnect();
}

} catch (Exception e) {
e.printStackTrace();
}
}
});

thread.start();

服务器.py

import webapp2


class MainHandler(webapp2.RequestHandler):

def post(self):
name = self.request.get('content')
self.response.out.write("Your name is: " + name)

def get(self):
self.response.write("Hello World")


app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True)

最佳答案

您的 name = self.request.get('content') 语句很可能没有达到您的预期。

由于发布数据是在消息正文中发送的,因此您可能需要查看 self.request.body (我不是 java 用户,我无法确切地说出您的发布数据是如何发送的在体内组织)。

来自 webapp2Request data :

POST data

Variables url encoded in the body of a request (generally a POST form submitted using the application/x-www-form-urlencoded media type) are available in request.POST. It is also a MultiDict and can be accessed in the same way as .GET. Examples:

request = Request.blank('/')
request.method = 'POST'
request.body = 'check=a&check=b&name=Bob'

# The whole MultiDict:
# POST([('check', 'a'), ('check', 'b'), ('name', 'Bob')])
post_values = request.POST

# The last value for a key: 'b'
check_value = request.POST['check']

# All values for a key: ['a', 'b']
check_values = request.POST.getall('check')

# An iterable with all items in the MultiDict:
# [('check', 'a'), ('check', 'b'), ('name', 'Bob')]
request.POST.items()

Like GET, the name POST is a somewhat misleading, but has historical reasons: they are also available when the HTTP method is PUT, and not only POST.

关于android - 如何从android接收和返回webapp2中的字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50691734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com