android-网络请求
java.net
Get请求
try { URL url = new URL(""); URLConnection connection = url.openConnection(); HttpURLConnection httpConnection = (HttpURLConnection)connection; httpConnection.setConnectTimeout(30 * 1000); httpConnection.connect(); if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { //这里使用的byte[]就相当于iOS中的NSData byte[] buffer = new byte[1024]; ByteArrayOutputStream outStream = new ByteArrayOutputStream(); InputStream inStream = httpConnection.getInputStream(); int len = 0; while ((len = inStream.read(buffer)) != -1) { //inputStream是写入了内存后的内容,比如从网络访问回来的结果或从硬盘中读取数据。outputStream是从内存中输出出去的内容,比如输出到网络或硬盘。 //这个while循环就是从网络中按字节为单位地将数据获取,inputStream的read是将网络获取下来的数据读到buffer并返回数据字节数。 //若果上次读入数据成功且是有数据返回,则继续申请获取数据,用outStream的write,并将上一次读入操作得到的字节数、缓存变量传入,让目标源知道这次该返回些什么数据。 outStream.write(buffer, 0, len); } inStream.close(); System.out.println(new String(outStream.toByteArray())); } httpConnection.disconnect(); } catch (Exception e) { System.out.println("get"+e.getLocalizedMessage()); }
Post请求
try { String path = "https://reg.163.com/logins.jsp"; String params = "id=" + URLEncoder.encode("helloworld", "UTF-8") + "&pwd=" + URLEncoder.encode("android", "UTF-8"); // 请求的参数转换为byte数组 byte[] postData = params.getBytes(); URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(30 * 1000); // Post请求必须设置允许输出 urlConnection.setDoOutput(true); urlConnection.setUseCaches(false); urlConnection.setRequestMethod("POST"); urlConnection.setInstanceFollowRedirects(true); urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencode"); urlConnection.connect(); //发送请求参数 DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream()); dos.write(postData); dos.flush(); dos.close(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { //xml解析(DOM) InputStream in = urlConnection.getInputStream(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document dom = db.parse(in); Element docEle = (Element) dom.getDocumentElement(); NodeList nl = docEle.getElementsByTagName("input"); if (nl != null && nl.getLength()>0) { for (int i = 0; i < nl.getLength(); i++) { Element input = (Element) nl.item(i); String name = input.getAttribute("name"); System.out.println("XML parse excice result : 163's input feild name"); } } //普通字节流获取 // ByteArrayOutputStream outStream = new ByteArrayOutputStream(); // InputStream inStream = urlConnection.getInputStream(); // int len = 0; // byte[] buffer = new byte[1024]; // while ((len = inStream.read(buffer)) != -1) { // outStream.write(buffer, 0, len); // } // inStream.close(); // System.out.println(new String(outStream.toByteArray())); } } catch (Exception e) { System.out.println("post"+e.getLocalizedMessage()); }
org.apache.http
Get请求
try { String path = ""; HttpGet httpGet = new HttpGet(path); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResp = httpClient.execute(httpGet); if (httpResp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { String result = EntityUtils.toString(httpResp.getEntity(),"UTF-8"); //JSON解析 String message = new JSONObject(result).getString("message"); System.out.println("HttpGet "+"message:"+message); } } catch (Exception e) { System.out.println("HttpGet "+e.getLocalizedMessage()); }
Post请求
try { String path = ""; HttpPost httpPost = new HttpPost(path); // Post参数 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("id", "helloworld")); params.add(new BasicNameValuePair("pwd", "android")); // 设置字符集 HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8); // 设置参数实体 httpPost.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); HttpResponse httpResp = httpClient.execute(httpPost); if (httpResp.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) { String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8"); } } catch (Exception e) { System.out.println("HttpPost "+e.getLocalizedMessage()); }