【问题标题】:HttpClient deprecated / Connect with PHP server - Android API 22HttpClient 已弃用/与 PHP 服务器连接 - Android API 22
【发布时间】:2015-09-17 13:16:59
【问题描述】:

我正在尝试登录并注册一个 Android 应用程序。 我在将代码调整为 API 22 时遇到问题。 虽然我知道我必须使用 HttpURLConnection 而不是 HttpRequestParams 等,并且已经这样做了,但我不知道如何调整代码以合并数据库服务器和存储在其中的 PHP 文件。

主要是下面这一点,我无法弄清楚。

HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");

有人可以帮忙吗?提前致谢。

这是完整的代码:

        @Override
        protected User doInBackground(Void... params) {

            ContentValues contentValues = new ContentValues();
            contentValues.put("username", user.username);
            contentValues.put("password", user.password);

            URL url = new URL(SERVER_ADDRESS);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(CONNECTION_TIMEOUT);
            conn.setReadTimeout(CONNECTION_TIMEOUT);

            HttpClient client = new DefaultHttpClient(httpRequestParams);
            HttpPost post = new HttpPost(SERVER_ADDRESS + "FetchUserData.php");

            User returnedUser = null;
            try {
                post.setEntity(new UrlEncodedFormEntity(dataToSend));
                HttpResponse httpResponse = client.execute(post);

                HttpEntity entity = httpResponse.getEntity();
                String result = EntityUtils.toString(entity);
                JSONObject jObject = new JSONObject(result);

                if(jObject.length() == 0) {
                    returnedUser = null;
                } else {
                    String mobile = jObject.getString("mobile");
                    String email = jObject.getString("email");

                    returnedUser = new User(mobile, email, user.mobile, user.email);
                }

            } catch(Exception e) {
                e.printStackTrace();
            }

            return returnedUser;
        }

【问题讨论】:

标签: android httpclient


【解决方案1】:

first:已经回答了 Sending Http request for Android 22+

第二:我已经做了一个满足你需求的类,它允许你用一行代码发送请求和接收响应(上面的帖子也有解释)

这是我的班级的链接:

HttpRequest

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多