android - android中的httppost

标签 android

我想将用户名和密码发送到 android 中的 php 页面。 我如何使用 httpost 执行此操作 即我想在 PHP 页面中使用 POST 获取值。 请给我一个例子。 谢谢

最佳答案

这也许是你想要的(评论是法语,抱歉)

        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 */ }
            }
        }

关于android - android中的httppost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3381523/

相关文章:

android - 替换默认的笑脸菜单

javascript - 可能未处理的 promise 拒绝 ID : Invalid FCM Event subscription

java - 修改 Settings.apk 以与 Project Glass 配合使用

Android PopupWindow 高程不显示阴影

android - 在SD卡上添加或删除或修改任何文件后,如何刷新或重新启动MediaStore?

android - 单击 EditText View 时,虚拟键盘会阻止下面的按钮

android - 在使用带有选项卡布局的自定义 View 时加载 Activity 时突出显示 View 寻呼机中的第一项?

android - 在 webview 和 chrome 之间共享 cookie

android - OpenGL - 无论屏幕尺寸/比例如何,都呈现相同的尺寸

android - HTML 嵌入视频背景不会在 Android 或 IOS 上加载