AsyncHttpClient使用示例代码
2018-07-20 来源:open-open
实现http异步请求get,post请求,也可以用作文件上传。 下面是登录的示例
1.声明一个client对象,初始化
AsyncHttpClient client = new AsyncHttpClient(); RequestParams params = new RequestParams(); params.add("RequestType", ""); params.add("UserID", "-1"); params.add("NurseID", "-1"); params.add("HospitalId", "-1"); JSONObject jsonObject = new JSONObject(); try { jsonObject.put("UserCode", username); jsonObject.put("Password", password); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } params.add("RequestParam", jsonObject.toString()); client.get(path, params, new TextHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { // TODO Auto-generated method stub if (statusCode == 200) { // Toast.makeText(getApplicationContext(), // responseString, Toast.LENGTH_SHORT) // .show(); Intent intent = new Intent(); intent.setClass( getApplicationContext(), MainActivity.class); startActivity(intent); } else { Intent intent = new Intent(); intent.setClass( getApplicationContext(), MainActivity.class); startActivity(intent); } } else { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { Toast.makeText(getApplicationContext(), "网络连接超时", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), responseString, Toast.LENGTH_SHORT).show(); }
1.声明一个client对象,初始化
2.根据需要调用client中get,post请求
3,重写onSuccess()和onFailure()方法
注:在https://github.com/AsyncHttpClient/async-http-client下载包,直接将library下的包直接粘贴到项目src下,直接调用即可
标签: 网络
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐