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

标签 android python google-app-engine webapp2

我正在努力将字符串接收到我的 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/

相关文章:

python - "What does this mean: key=lambda x: x[-1] ?"

python - 当有人注册时,WTForms 可以检查两个密码是否相同

python - 在 SQLAlchemy 中注释 `exists` 子查询

python - PyCall import ("numpy") 产生 MKL FATAL ERROR

google-app-engine - 如何在 App Engine Go 日志中自动记录函数/行号?

google-app-engine - 在 Google App Engine (Java) 中限制 blobstore 上传的文件类型

Android:检测用户是否在公共(public)汽车/火车/电车/等上

android将工具栏标题颜色更改为白色

android - NestedScrollView 在 Nougat (API 25) 上抛出停止错误

java - 旋转 Android 应用程序的文本和图标