如何从 android 接收 webapp2 中的 return 字符串?
How to receive and return string in webapp2 from android?
我很难将字符串接收到我的 webapp2 服务器中。
android 设备应该发送字符串 name
到服务器,然后服务器 returns 返回到 phone "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();
SERVER.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')
声明很可能没有达到您的预期。
由于 post 数据是在邮件正文中发送的,因此您可能想查看 self.request.body
(我不是 java 用户,我无法确切地说出如何您的 post 数据在正文中组织)。
来自 webapp2
的 Request 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.
我很难将字符串接收到我的 webapp2 服务器中。
android 设备应该发送字符串 name
到服务器,然后服务器 returns 返回到 phone "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();
SERVER.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')
声明很可能没有达到您的预期。
由于 post 数据是在邮件正文中发送的,因此您可能想查看 self.request.body
(我不是 java 用户,我无法确切地说出如何您的 post 数据在正文中组织)。
来自 webapp2
的 Request 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 inrequest.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 namePOST
is a somewhat misleading, but has historical reasons: they are also available when the HTTP method is PUT, and not only POST.