Java 从google应用程序引擎向php发送post请求
我在GAE中有一些servlet,是从Android应用程序调用的;我想使用xampp将这些servlet之一的POST请求发送到本地主机中托管的php。servlet在尝试读取响应时遇到IOException 这是我正在使用的示例servlet的代码:Java 从google应用程序引擎向php发送post请求,java,php,android,google-app-engine,servlets,Java,Php,Android,Google App Engine,Servlets,我在GAE中有一些servlet,是从Android应用程序调用的;我想使用xampp将这些servlet之一的POST请求发送到本地主机中托管的php。servlet在尝试读取响应时遇到IOException 这是我正在使用的示例servlet的代码: protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOExcepti
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String result = "";
try {
URL url = new URL("http://172.25.3.50:80/tempofinito/prueba.php");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
// Send
DataOutputStream wr = new DataOutputStream (
con.getOutputStream ());
wr.writeBytes ("prueba=" + URLEncoder.encode("message","UTF-8"));
wr.flush ();
wr.close ();
// Response
InputStream is = con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer resp = new StringBuffer();
while((line = rd.readLine()) != null) {
resp.append(line);
resp.append('\r');
}
rd.close();
result = resp.toString();
} catch (MalformedURLException e) {
result = "malformed";
} catch (IOException e) {
result = "ioexception";
}
// Sends result to Android APP
PrintWriter out = response.getWriter();
out.println(result);
}
这是php文件:
<?php
$variable = $_POST["prueba"];
echo "ESTO ES UNA PRUEBA ".$variable;
?>
这是Android代码:
new AsyncTask<Void, Void, String>() {
protected String doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(Globals.serverURL + "/prueba");
String result = "";
try {
// Ignore this ->
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
nameValuePairs.add(new BasicNameValuePair("UserName", Globals.user));
nameValuePairs.add(new BasicNameValuePair("Pass", Globals.encrypt(Globals.pass)));
nameValuePairs.add(new BasicNameValuePair("Mode", "user"));
// <-
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(postMethod);
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
newasynctask(){
受保护字符串doInBackground(Void…params){
HttpClient=new DefaultHttpClient();
HttpPost postMethod=新的HttpPost(Globals.serverURL+“/prueba”);
字符串结果=”;
试一试{
//忽略此->
ArrayList nameValuePairs=新的ArrayList(3);
添加(新的BasicNameValuePair(“用户名”,Globals.user));
添加(新的BasicNameValuePair(“Pass”,Globals.encrypt(Globals.Pass));
添加(新的BasicNameValuePair(“模式”、“用户”);
//防火墙出现了一个愚蠢的问题,它阻塞了连接