yy1.2.3.使用HttpUitls提交get,Post请求 xUtils HttpUtils GET POST
1 public class MainActivity extends Activity { 2 3 private String PATH = "https://www.baidu.com/"; 4 private String PATH_ERROR = "www.baidu.com/"; 5 6 private TextView mTv; 7 8 private HttpUtils mHttpUtils; 9 10 @Override 11 protected void onCreate(Bundle savedInstanceState) { 12 super.onCreate(savedInstanceState); 13 setContentView(R.layout.activity_main); 14 15 mTv = (TextView) findViewById(R.id.tv); 16 17 mHttpUtils = new HttpUtils(); 18 19 // 设置当前HttpUtils实例缓存超时的时间,以毫秒为单位 20 mHttpUtils.configCurrentHttpCacheExpiry(1); 21 22 // 设置所有HttpUtils缓存超时的时间 23 // mHttpUtils.configDefaultHttpCacheExpiry(5000); 24 25 // 设置缓存的大小 26 // mHttpUtils.configHttpCacheSize(httpCacheSize) 27 28 // 设置同一时间可以发起网络请求的数据量 29 mHttpUtils.configRequestThreadPoolSize(5); 30 31 // 设置请求超时的时间 32 mHttpUtils.configTimeout(25000); 33 } 34 35 public void request(View view) { 36 mHttpUtils.send(HttpMethod.GET, PATH_ERROR, new RequestCallBack<String>() { 37 38 @Override 39 public void onSuccess(ResponseInfo<String> responseInfo) { 40 String data = responseInfo.result; 41 42 mTv.setText(data); 43 44 Log.i("bm", responseInfo.reasonPhrase + " reasonPhrase"); 45 Log.i("bm", responseInfo.contentEncoding + " contentEncoding"); 46 Log.i("bm", responseInfo.contentLength + " contentLength"); 47 Log.i("bm", responseInfo.contentType + " contentType"); 48 Log.i("bm", responseInfo.locale + " locale"); 49 Log.i("bm", responseInfo.protocolVersion + " protocolVersion"); 50 Log.i("bm", responseInfo.resultFormCache + " resultFormCache"); 51 Log.i("bm", responseInfo.statusCode + " statusCode"); 52 } 53 54 @Override 55 public void onFailure(HttpException error, String msg) { 56 Log.i("bm", msg); 57 } 58 }); 59 } 60 }
1 public class MainActivity extends Activity { 2 3 private String PATH = "https://www.baidu.com/"; 4 5 public static final String BAIKEPATH = "http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=news.getListByType"; 6 7 private TextView mTv; 8 9 private HttpUtils mHttpUtils; 10 11 private HttpHandler<String> mHttpHandler; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 mTv = (TextView) findViewById(R.id.tv); 19 20 mHttpUtils = new HttpUtils(); 21 22 // 璁剧疆褰撳墠HttpUtils瀹炰緥缂撳瓨瓒呮椂鐨勬椂闂达紝浠ユ绉掍负鍗曚綅 23 mHttpUtils.configCurrentHttpCacheExpiry(1); 24 } 25 26 public void request(View view) { 27 RequestParams params = new RequestParams(); 28 29 params.addBodyParameter("rows", "15"); 30 params.addBodyParameter("page", "0"); 31 params.addBodyParameter("type", "16"); 32 33 mHttpHandler = mHttpUtils.send(HttpMethod.POST, BAIKEPATH, params, new RequestCallBack<String>() { 34 35 @Override 36 public void onSuccess(ResponseInfo<String> responseInfo) { 37 mTv.setText(responseInfo.result); 38 } 39 40 @Override 41 public void onFailure(HttpException error, String msg) { 42 43 } 44 45 @Override 46 public void onStart() { 47 Log.i("bm", "onStart"); 48 } 49 50 @Override 51 public void onCancelled() { 52 Log.i("bm", "onCancelled"); 53 } 54 55 @Override 56 public void onLoading(long total, long current, boolean isUploading) { 57 Log.i("bm", total + " " + current + " " + isUploading); 58 } 59 }); 60 } 61 62 public void cancel(View view) { 63 mHttpHandler.cancel(); 64 } 65 }
posted on 2015-04-07 18:58 CodeUtils 阅读(1985) 评论(0) 编辑 收藏 举报