IOException when sending post request to a php from google app engine

73 views
Skip to first unread message

alfonsoescr...@gmail.com

unread,
Aug 30, 2013, 4:28:18 AM8/30/13
to google-a...@googlegroups.com

I have some servlets in GAE, called from an Android app; and I want to send a POST request from one of these servlets to a php hosted in localhost using xampp. The servlet reaches an IOException when trying to read the response.

This is the code of a the sample servlet i am using:

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

This is the php file:

<?php
$variable = $_POST["prueba"];
echo
"ESTO ES UNA PRUEBA ".$variable;
?>

And this is the Android code:

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

So, the APP calls the servlet "prueba". This servlet just tries to send a POST request to the php file, but reaches an IOException in the "//Response" part. I suppose I'm doing something wrong because if I copy the same code from the servlet and paste it in the Android APP, instead of the code above, it works fine.

Should I do it in a different way inside Google App Engine? What I want is to communicate a servlet in GAE with an external server which will interact with an external database. I can't use Google Cloud Storage because the database should be in that external server.



Vinny P

unread,
Sep 1, 2013, 1:33:34 PM9/1/13
to google-a...@googlegroups.com
On Fri, Aug 30, 2013 at 3:28 AM, <alfonsoescr...@gmail.com> wrote:

I have some servlets in GAE, called from an Android app; and I want to send a POST request from one of these servlets to a php hosted in localhost using xampp.



When you say localhost, where is the host local to? Is localhost referring to App Engine, the Android app, or another machine? 

 
 
-----------------
-Vinny P
Technology & Media Advisor
Chicago, IL

App Engine Code Samples: http://www.learntogoogleit.com
 
 

Reply all
Reply to author
Forward
0 new messages