JAVA中只有有几种HttpURLConnection 和HttpClient

在Android也可以使用,但是Android封装了2种新的网络请求方式:分别是volleyandroid-async-http

   

第一种:HttpURLConnection:

GET方式:

URL url = new URL("http://fanyi.youdao.com/openapi.do?keyfrom=fadabvaa&key=522071532&type=data&doctype=xml&version=1.1&q=Americans");URLConnection connection = url.openConnection();//connection.addRequestProperty("encoding", "UTF-8");InputStream is =connection.getInputStream();//字符流InputStreamReader isr =new InputStreamReader(is,"UTF-8");//字节流BufferedReader bfr =new BufferedReader(isr);String line;StringBuffer sb =new StringBuffer();while ((line=bfr.readLine())!=null) {sb.append(line);}is.close();isr.close();bfr.close();System.out.println(sb.toString());POST方式:

URL url =new URL("http://fanyi.youdao.com/openapi.do");HttpURLConnection conection=(HttpURLConnection) url.openConnection();conection.addRequestProperty("encding", "UTF-8");conection.setDoOutput(true);conection.setDoInput(true);conection.setRequestMethod("POST");//输出流 可以采用匿名方式 OutputStream os =conection.getOutputStream();OutputStreamWriter osw =new OutputStreamWriter(os);BufferedWriter bw =new BufferedWriter(osw);bw.write("keyfrom=fadabvaa&key=522071532&type=data&doctype=xml&version=1.1&q=China");bw.flush(); //输入流 可以采用匿名方式 InputStream is =conection.getInputStream();InputStreamReader isr =new InputStreamReader(is);BufferedReader br =new BufferedReader(isr);String line;StringBuffer sb =new StringBuffer();while ((line =br.readLine())!=null) {sb.append(line);}os.close();osw.close();bw.close();is.close();isr.close();br.close();System.out.println(sb.toString());

第二种是:HttpClient 中的HttpGet:

HttpClient httpclient =HttpClients.createDefault();@Overridepublic void run() {HttpGet get =new HttpGet("http://fanyi.youdao.com/openapi.do?keyfrom=fadabvaa&key=522071532&type=data&doctype=xml&version=1.1&q=Americans");try {HttpResponse response =httpclient.execute(get);HttpEntity entity=response.getEntity();String result =EntityUtils.toString(entity,"UTF-8");System.out.println(result);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}HttpPost:

HttpClient httpclient = HttpClients.createDefault();@Overridepublic void run() {     HttpPost post = new HttpPost("http://fanyi.youdao.com/openapi.do");try {java.util.List  parameters=new ArrayList<>();parameters.add(new BasicNameValuePair("keyfrom", "fadabvaa"));parameters.add(new BasicNameValuePair("key", "522071532"));parameters.add(new BasicNameValuePair("type", "data"));parameters.add(new BasicNameValuePair("doctype", "xml"));parameters.add(new BasicNameValuePair("version", "1.1"));parameters.add(new BasicNameValuePair("q", "Americans"));post.setEntity(new UrlEncodedFormEntity(parameters,"UTF-8"));HttpResponse response = httpclient.execute(post);HttpEntity entity = response.getEntity();if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {String result = EntityUtils.toString(entity, "UTF-8");System.out.println(result);}} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}第三种Android   volley

GET:

String url = "http://apis.juhe.cn/mobile/get?phone=13666666666&key=335adcc4e891ba4e4be6d7534fd54c5d";StringRequest request =new StringRequest(Request.Method.GET, url, new Response.Listener() {    @Override    public void onResponse(String s) {        Toast.makeText(MainActivity.this, s,                Toast.LENGTH_LONG).show();    }},new Response.ErrorListener(){    @Override    public void onErrorResponse(VolleyError volleyError) {        Toast.makeText(MainActivity.this, "网络请求失败",                Toast.LENGTH_LONG).show();    }});request.setTag("abcGet");MyApplication.queue.add(request);
POST:
StringRequest request = new StringRequest(Method.POST, url,  listener, errorListener) {      @Override      protected Map getParams() throws AuthFailureError {          Map map = new HashMap();          map.put("params1", "value1");          map.put("params2", "value2");          return map;      }  };
   
 第四种:   

android-async-http



         





更多相关文章

  1. GitHub 标星 2.5K+!教你通过玩游戏的方式学习 VIM!
  2. Nginx系列教程(六)| 手把手教你搭建 LNMP 架构并部署天空网络电影
  3. android ndk build
  4. android wap连接网络设置代理
  5. android sdk manager 无法更新
  6. Android获取Contact Number的例子(2.0系统以前的获取方式)
  7. Android(安卓)6.0+ TelephonyManager 使用示例(1) 获取网络和 SIM
  8. MTK Android(安卓)平台开发之旅
  9. Android(安卓)OpenGL ES 开发中的Buffer使用

随机推荐

  1. where条件顺序不同、性能不同示例探讨
  2. 如何将sql执行的错误消息记录到本地文件
  3. sql中case语句的用法浅谈
  4. SQL Server 数据库基本操作语句总结
  5. SQL Server 数据库分离与附加(图文教程)
  6. CMD命令操作MSSQL2005数据库(命令整理)
  7. SQL有外连接的时候注意过滤条件位置否则
  8. sql to sqlalchemy 转换的小例子
  9. android 相关2
  10. android webview点击返回键回到上一个htm