如何在安卓和GAE-python之间发送和接收数据

2024-05-31 23:46:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我用这段代码将数据发送到部署了gae python应用程序的站点,但我不知道如何在另一端接收数据。在

protected void tryLogin(String mUsername, String mPassword)
    {           
        HttpURLConnection connection;
       OutputStreamWriter requestself = null;

            URL url = null;   
            String response = null;         
         String parameters = "username="+mUsername+"&password="+mPassword;   

            try

            {
                url = new URL("http://www.pranshutrial3.appspot.com");
                connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.setRequestMethod("POST");    

                requestself = new OutputStreamWriter(connection.getOutputStream());
                requestself.write(parameters);
                requestself.flush();
                requestself.close();            
                String line = "";               
                InputStreamReader isr = new InputStreamReader(connection.getInputStream());
                BufferedReader reader = new BufferedReader(isr);
                StringBuilder sb = new StringBuilder();
                while ((line = reader.readLine()) != null)
                {
                    sb.append(line + "\n");
                }
                // Response from server after login process will be stored in response variable.                
                response = sb.toString();
                // You can perform UI operations here
                Toast.makeText(this,"Message from Server: \n"+ response, Toast.LENGTH_LONG).show();             
                isr.close();
                reader.close();

            }
            catch(IOException e)
            {
                // Error
            }


    }

Tags: urlnewclosestringresponselineconnectionnull