JAVA中只有有几种HttpURLConnection 和HttpClient
在Android也可以使用,但是Android封装了2种新的网络请求方式:分别是volley和android-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 =newStringRequest(Request.Method.GET, url, newResponse.Listener() {
@Overridepublic voidonResponse(String s) {
Toast.makeText(MainActivity.this, s,
Toast.LENGTH_LONG).show();
}
},newResponse.ErrorListener(){
@Overridepublic voidonErrorResponse(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