Android学习-Http请求
2016-03-01 23:55
337 查看
今天继续学习网络编程模块。首先是WebView,简单的来访问网页
在这之前先要什么访问网络的权限
获取代码的时候首先新建一个URL表示要访问的网址,然后利用URL里的openConnection()方法来新建HttpURLConnection对象,接着获取inputStream输入流,用来获取服务器返回的代码数据,然后调用HttpURLConnection的setRequestMethod()方法来设置Http请求的方法,因为我们这里是获取数据,所以传入”GET”(发送就用POST),然后再将inputStream的数据转换成String字符串显示出来
发起网络请求,必须开启子线程
execute()方法会返回一个HttpResponse对象,里面就包含服务器返回给我们的对象,HttpResponse.getEntity()可以取得返回的具体内容,然后利用EntityUtils.toString(entity)就可以得到返回的字符串数据。
Handler部分和HttpURLConnection一样。
在这之前先要什么访问网络的权限
<uses-permission android:name="android.permission.INTERNET"/>
WebView
实现在layout中新建webview组件,然后在activity中新建webview对象,然后调用setWebViewClient()方法,该方法中传入一个WebViewClient对象webView=(WebView) findViewById(R.id.webview); webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub view.loadUrl(url); return true; } }); webView.loadUrl("http://www.baidu.com");
HttpURLConnection
用来获取百度的HTML代码,首先新建子线程,在子线程中来访问服务器数据并获取HTML代码,然后利用Handler来进行UI操作private static final int GET_HTTPREQUEST=0; private Handler handler=new Handler(){ public void handleMessage(Message msg) { switch (msg.what) { case GET_HTTPREQUEST: String info=(String) msg.obj; textView.setText(info); break; default: break; } } };
获取代码的时候首先新建一个URL表示要访问的网址,然后利用URL里的openConnection()方法来新建HttpURLConnection对象,接着获取inputStream输入流,用来获取服务器返回的代码数据,然后调用HttpURLConnection的setRequestMethod()方法来设置Http请求的方法,因为我们这里是获取数据,所以传入”GET”(发送就用POST),然后再将inputStream的数据转换成String字符串显示出来
private void requestUrl() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub URL url=null; try { url = new URL("http://www.baidu.com"); HttpURLConnection connection=(HttpURLConnection) url.openConnection(); InputStream in=connection.getInputStream(); connection.setRequestMethod("GET"); connection.setConnectTimeout(8000); connection.setReadTimeout(8000);//设置超时选项 StringBuilder sb=new StringBuilder(); String line; BufferedReader reader=new BufferedReader(new InputStreamReader(in)); while((line=reader.readLine())!=null) { sb.append(line); } Message message=new Message(); message.what=GET_HTTPREQUEST; message.obj=sb.toString(); handler.sendMessage(message); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); }
发起网络请求,必须开启子线程
HttpClient
GET
HttpClient是一个接口,所以无法实例化,采用new一个DefaultHttpClient的实例来创建,然后新建一个HttpGet对象来表示我们需要访问的网址,接着调用HttpClient的execute()方法即可完成访问请求。execute()方法会返回一个HttpResponse对象,里面就包含服务器返回给我们的对象,HttpResponse.getEntity()可以取得返回的具体内容,然后利用EntityUtils.toString(entity)就可以得到返回的字符串数据。
private void requestUrlClien() { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { HttpClient httpClient=new DefaultHttpClient(); HttpGet httpGet=new HttpGet("http://www.baidu.com"); HttpResponse httpResponse=httpClient.execute(httpGet); if (httpResponse.getStatusLine().getStatusCode()==200) {//等于200说明请求成功 HttpEntity entity=httpResponse.getEntity(); String response=EntityUtils.toString(entity,"utf-8"); Message message=new Message(); message.what=GET_HTTPREQUEST; message.obj=response; handler.sendMessage(message); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }).start(); }
Handler部分和HttpURLConnection一样。
POST
同样新建HttpClient,然后新建HttpPost对象,传入网址,然后通过NameValuePair的List来存储要提交上去的参数,调用add(new BasicNameValuePair())来添加参数,然后将List集合传入UrlEncodedFormEntity对象中,最后调用HttpPost.setEntity();List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new 4000 BasicNameValuePair("username", "admin")); params.add(new BasicNameValuePair("password", "123456")); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, "utf-8"); httpPost.setEntity(entity); httpClient.execute(httpPost);
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories