开发者

Httpost in android

I want to send a username and password to a php page in android. how do i do this using httpost i.开发者_Go百科e i want to get the values using POST in the PHP page. Please give me an example. Thankyou


Here is maybe what you want ( comments are in french, sorry)

        HttpClient client = new HttpClient();

        HttpClientParams clientParams = new HttpClientParams();
        clientParams.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
        client.setParams(clientParams);

        // Le HTTPMethod qui sera un Post en lui indiquant l'URL du traitement du formulaire
        PostMethod methode = new PostMethod("http://mywebsite/php/messages.php");
        // On ajoute les parametres du formulaire
        methode.addParameter("password", "my_password");
        methode.addParameter("user_name", userName);
        // Le buffer qui nous servira a recuperer le code de la page
        BufferedReader br = null;
        String txt = null;
        try
        {
            // http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpStatus.html
            int retour = client.executeMethod(methode);
            // Pour la gestion des erreurs ou un debuggage, on recupere le nombre renvoye.
            //System.out.println("La reponse de executeMethod est : " + retour);
            br = new BufferedReader(new InputStreamReader(methode.getResponseBodyAsStream()));
            String readLine;


            // Tant que la ligne en cours n'est pas vide
            while(((readLine = br.readLine()) != null))
                {
                    txt+=readLine;
                }
        }
        catch (Exception e)
        {
            System.err.println(e); // erreur possible de executeMethod
        }
        finally
        {
        // On ferme la connexion
        methode.releaseConnection();
        if(br != null)
            {
            try
                {
                br.close(); // on ferme le buffer
                }
            catch (Exception e) { /* on fait rien */ }
            }
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜