public String getXmlFromServer(final Context usingcontext,
String method, String sentence) {
String path = usingcontext.getString(R.string.server_path) + method;
Log.e("111", path);
RequestParams params = new RequestParams();
params.put("sentence", sentence);
AsyncHttpClient client = new AsyncHttpClient();
client.post(path, params, new TextHttpResponseHandler() {
@SuppressWarnings("deprecation")
@Override
// 请求成功
public void onSuccess(int statusCode, Header[] headers,
String response) {
super.onSuccess(statusCode, headers, response);
SystemClock.sleep(1000);
stringResult = response.toString();
for (int i = 0; i < stringResult.length(); i++) {
if (stringResult.charAt(i) != ' ') {
stringResult = stringResult.substring(i, stringResult.length());
break;
}
}
stringResult = stringResult.trim();
Toast.makeText(usingcontext, stringResult, Toast.LENGTH_LONG)
.show();
}
@Override
@Deprecated
public void onFailure(int context, Header[] headers,
Throwable error, String content) {
Toast.makeText(usingcontext, "服务器忙请稍后重试", Toast.LENGTH_LONG)
.show();
}
});
return stringResult;
}
求解
Android官方推荐的有 HttpClient 和 HttpURLConnection;
当时,2.1,2.2时用Apache的HttpClient;2.3之后的建议用HttpURLConnection.
目前,开源的比较流行的有:
1、volley
2、android-async-http
3、retrofit
4、okhttp
5、androidquery
6、AndroidAsync
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。