2013-08-28 78 views
0

我有一些GAE中的servlet,从Android应用中调用;我想从这些servlet之一发送POST请求到使用xampp在本地主机上托管的php。尝试读取响应时,servlet会发生IOException。从谷歌应用引擎发送POST请求到一个PHP

这是我使用的是样本servlet的代码:

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; 
     } 

所以,APP调用servlet“prueba”。这个servlet只是试图发送一个POST请求到php文件,但是在“// Response”部分中出现IOException。我想我做错了什么,因为如果我从servlet复制相同的代码并将其粘贴到Android应用程序中,而不是上面的代码,它可以正常工作。

我应该以不同的方式在Google App Engine中执行此操作吗?

回答

1

这是一个阻碍连接的防火墙的一个愚蠢的问题。

相关问题