【发布时间】:2015-07-11 19:25:28
【问题描述】:
我正在学习一个教程,但我发现很多代码都被弃用了。
ArrayList<NameValuePair> dataToSend = new ArrayList<>();
dataToSend.add(new BasicNameValuePair("name", user.name));
dataToSend.add(new BasicNameValuePair("age", user.age));
HttpParams httpRequestParams = new BasicHttpParams();
HttpConnectionParamas.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpConnectionParamas.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT);
HttpClient client = new DefaultHttpClient(httpRequestParams);
HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php");
try{
post.setEntity(new UrlEncodedFormEntity(dataToSend));
client.execute(post);
}catch (Exception e){
e.printStackTrace();
}
还有另一个返回结果的 POST 方法
HttpResponse httpResponse = client.execute(post);
HttpEntity entity = httpResponse.getEntity();
String result = EntityUtils.toString(entity);
JSONObject jObject = new JSONObject(result);
我发现我可以用
替换NameValuePair
ContentValues values = new ContentValues();
values.put("name", user.name);
values.put("age", user.age + "");
但我不知道其他人。
【问题讨论】:
标签: android android-studio androidhttpclient