HttpClient 已弃用/连接 PHP 服务器 - Android API 22
HttpClient deprecated / Connect with PHP server - Android API 22
我正在尝试登录并注册 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;
}
第一:已经回答了
其二:我做了一个满足你需求的class,一行代码就可以发送请求和接收响应(上面post中也有说明)
这是我的 class 的 link:
我正在尝试登录并注册 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;
}
第一:已经回答了
其二:我做了一个满足你需求的class,一行代码就可以发送请求和接收响应(上面post中也有说明)
这是我的 class 的 link: