android中的网络操作和java里面没有什么区别,java里面的很多网络操作方法都可以搬到android中去使用,主要几个点:

1、post和get请求的区别,大家可以在网上查阅有关资料进行了解,get主要以向地址中拼接字符串参数发送到服务器,长度有限制,并且请求参数暴露在地址栏中,不怎么安全;post则主要是将请求参数转换为相应的http协议请求体发送到服务器,相比get方式,参数的长度没有限制,并且参数信息不会暴露给用户;

2、我们在java web里面通过浏览器以post方式发送数据,浏览器帮我们转换为相应的http协议,也就是说浏览器内部帮我们设置为相应的http请求体,然后发送到服务器,用户通过浏览器向服务器发送数据-->浏览器转换为http协议-->发送到服务器,如果我们用android做客户端用java代码编写post请求,需要设置其请求体,而get方式请求则只需要拼接相应的参数地址,用户通过android客户端向服务器发送数据-->编码设置为相应的http协议-->发送到服务器;

3、android里面包含了apache的httpclient,相当与web里面的浏览器,我们还可以利用其api进行相应的请求操作,相比与纯java代码,它里面封装了很多东西,我们只需要根据业务需求选择相应的api即可;如果我们在swing中使用apache的httpclient,则需要导入其相应的jar包

post请求:

java web:用户通过浏览器向服务器发送数据-->浏览器转换为http协议-->发送到服务器

java代码:用户通过android客户端向服务器发送数据-->编码设置为相应的http协议-->发送到服务器

httpclient:用户通过android客户端向服务器发送数据(httpclient帮我们转换为相应的http协议)-->发送到服务器

代码实现功能:里面都有相应的注释,我在我的工程里面建立了一个专门处理Http上传、下载的类:HttpUtil

1、从服务器获取二进制数据,图片、视频等:

/** * 从服务器上获取二进制数据,例如图片、视频等 * @param serverUrl: 服务器地址 * @param newFileName:获取到本地后新文件的地址+名称 例如:C://test.png */public static void getByteFromServer(String serverUrl,String newFileName)throws Exception{URL url = new URL(serverUrl);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");//通过Get方式请求conn.setConnectTimeout(5 * 1000);//设置连接延迟,因为android系统内存有限,不能长时间去与服务器建立连接InputStream inStream = conn.getInputStream();//通过输入流获取图片数据byte[] data = readInputStream(inStream);//得到图片的二进制数据File imageFile = new File(newFileName);FileOutputStream outStream = new FileOutputStream(imageFile);outStream.write(data);outStream.close();}

/** * 将输入流转换为字节数据 * @param inStream * @return * @throws Exception */public static byte[] readInputStream(InputStream inStream) throws Exception{ByteArrayOutputStream outStream = new ByteArrayOutputStream();byte[] buffer = new byte[1024];int len = 0;while( (len=inStream.read(buffer)) != -1 ){outStream.write(buffer, 0, len);}inStream.close();return outStream.toByteArray();}

2、从服务器获取文本信息,例如文本、html、xml、json等数据信息

/** * 从服务器上获取文本信息,例如html、xml等信息 * @param serverUrl * @return 文本字符串 */public static String getTxtFromServer(String serverUrl)throws Exception{URL url = new URL(serverUrl);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);InputStream inStream = conn.getInputStream();//通过输入流获取txt数据byte[] data = readInputStream(inStream);//得到txt的二进制数据String txt = new String(data);return txt;}

3、以get方式向服务器发送数据信息

/**Get请求方式即是通过将参数放到地址中发送到服务器 * 向服务器发送Get请求 * @param path:服务器请求地址 * @param params:请求参数 * @param enc:编码 * @return true:请求成功  false:请求失败 * @throws Exception */public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{StringBuilder sb = new StringBuilder(path);//?username=jack&password=123456&age=23if(params!=null && !params.isEmpty()){sb.append('?');for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);}URL url = new URL(sb.toString());HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);if(conn.getResponseCode()==200){//请求成功,可以调用上面的getByteFromServer方法得到相应的服务器数据//byte[] b=readInputStream(conn.getInputStream());return true;}return false;}

4、以post方式向服务器发送数据信息:

/**Post请求在浏览器中是转换了相应的http协议进行 * 而转换后的http协议中包含了例如Content-Type、Content-Length等信息 * 所以通过android客户端发送post请求需要将其设置为相应的http协议发送出去 * 向服务器发送一个post表单请求 * @param path:服务器地址 * @param params:请求参数 * @param enc:编码 * @return true:请求成功 false:请求失败 * @throws Exception */public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{//username=jack&password=123456&age=23StringBuilder sb = new StringBuilder();if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);}byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5 * 1000);conn.setUseCaches(false);//不进行缓存conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据//设置http请求头,向服务器发送post请求,Content-Type和Content-Length两个参数必须要,其他可省略//设置发送内容类型,为表单数据conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置发送内容长度conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));conn.setRequestProperty("Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");conn.setRequestProperty("Accept-Language", "zh-CN");conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");conn.setRequestProperty("Connection", "Keep-Alive");OutputStream outStream = conn.getOutputStream();outStream.write(entitydata);outStream.flush();outStream.close();if(conn.getResponseCode()==200){//请求成功,可以调用上面的getByteFromServer方法得到相应的服务器数据//byte[] b=readInputStream(conn.getInputStream());return true;}return false;}

5、以post方式向服务器发送xml数据信息:

/** * 向服务器发送xml数据 * @param path:服务器地址 * @param xml:xml字符串信息 * @param enc:编码 * @return true:请求成功  false:请求失败 * @throws Exception */public static boolean sendPostXMLRequest(String path, String xml,String enc)throws Exception{byte[] data = xml.getBytes();URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5 * 1000);conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据//设置发送内容类型,为xml数据conn.setRequestProperty("Content-Type", "text/xml; charset="+enc);//设置发送内容长度conn.setRequestProperty("Content-Length", String.valueOf(data.length));OutputStream outStream = conn.getOutputStream();outStream.write(data);outStream.flush();outStream.close();if(conn.getResponseCode()==200){//byte[] b=readInputStream(conn.getInputStream());return true;}return false;}

6、通过HttpClient以get方式向服务器发送数据:

/** * 通过httpClient发送get请求 * @param path:服务器地址 * @param params:参数名称 * @param enc:编码 * @return true:请求成功  false:请求失败 * @throws Exception */public static boolean sendGetRequestFromHttpClient(String path, Map<String, String> params,String enc)throws Exception{StringBuilder sb = new StringBuilder(path);//?username=jack&password=123456&age=23if(params!=null && !params.isEmpty()){sb.append('?');for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);}//相当与浏览器    HttpClient httpClient=new DefaultHttpClient();//得到一个HttpGet对象HttpGet get=new HttpGet(sb.toString());//发送一个Get请求HttpResponse response=httpClient.execute(get);if(response.getStatusLine().getStatusCode()==200){//String str=EntityUtils.toString(response.getEntity());即可得到服务器数据信息//InputStream inStream=httpEntity.getContent();即可得到服务器输入流return true;}return false;}

7、通过HttpClient以post方式向服务器发送请求:

/** * 通过httpClient发送Post请求 * @param path:服务器地址 * @param params:参数名称 * @param enc:编码 * @return true:请求成功  false:请求失败 * @throws Exception */public static boolean sendPostRequestFromHttpClient(String path, Map<String, String> params,String enc)throws Exception{List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据HttpPost post = new HttpPost(path); //formpost.setEntity(entitydata);DefaultHttpClient client = new DefaultHttpClient(); //浏览器HttpResponse response = client.execute(post);//执行请求if(response.getStatusLine().getStatusCode()==200){//String str=EntityUtils.toString(response.getEntity());即可得到服务器数据信息//InputStream inStream=httpEntity.getContent();即可得到服务器输入流return true;}return false;}

8、通过post发送上传文件信息到服务器,相当与web表单提交,表单中包含上传文件,其实根据浏览器在上传表单的时候它底层转换为相应的http协议,我们也可以模仿其编写相应的请求体然后提交给服务器即可,了解其原理了就很容易去理解了;

建立表单实体文件:

public class FormFile {/* 上传文件的数据 */private byte[] data;private InputStream inStream;private File file;/* 文件名称 */private String filname;/* 请求参数名称*/private String parameterName;/* 内容类型 */private String contentType = "application/octet-stream";public FormFile(String filname, byte[] data, String parameterName, String contentType) {this.data = data;this.filname = filname;this.parameterName = parameterName;if(contentType!=null) this.contentType = contentType;}public FormFile(String filname, File file, String parameterName, String contentType) {this.filname = filname;this.parameterName = parameterName;this.file = file;try {this.inStream = new FileInputStream(file);} catch (FileNotFoundException e) {e.printStackTrace();}if(contentType!=null) this.contentType = contentType;}public File getFile() {return file;}public InputStream getInStream() {return inStream;}public byte[] getData() {return data;}public String getFilname() {return filname;}public void setFilname(String filname) {this.filname = filname;}public String getParameterName() {return parameterName;}public void setParameterName(String parameterName) {this.parameterName = parameterName;}public String getContentType() {return contentType;}public void setContentType(String contentType) {this.contentType = contentType;}}

发送包含File请求:

/** * 向服务器提交的信息包含上传文件信息 * 直接通过HTTP协议提交数据到服务器,实现如下面表单提交功能: *   <FORM METHOD=POST ACTION="http://192.168.0.200:8080/ssi/fileload/test.do" enctype="multipart/form-data"><INPUT TYPE="text" NAME="name"><INPUT TYPE="text" NAME="id"><input type="file" name="imagefile"/>    <input type="file" name="zip"/> </FORM> * @param path 上传路径(注:避免使用localhost或127.0.0.1这样的路径测试,因为它会指向手机模拟器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080这样的路径测试) * @param params 请求参数 key为参数名,value为参数值 * @param file 上传文件 */public static boolean sendPostRequest(String path, Map<String, String> params, FormFile[] files) throws Exception{             final String BOUNDARY = "---------------------------7da2137580612"; //数据分隔线        final String endline = "--" + BOUNDARY + "--\r\n";//数据结束标志                int fileDataLength = 0;        for(FormFile uploadFile : files){//得到文件类型数据的总长度        StringBuilder fileExplain = new StringBuilder();         fileExplain.append("--");         fileExplain.append(BOUNDARY);         fileExplain.append("\r\n");         fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");         fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");         fileExplain.append("\r\n");         fileDataLength += fileExplain.length();        if(uploadFile.getInStream()!=null){        fileDataLength += uploadFile.getFile().length();     }else{     fileDataLength += uploadFile.getData().length;     }        }        StringBuilder textEntity = new StringBuilder();        for (Map.Entry<String, String> entry : params.entrySet()) {//构造文本类型参数的实体数据            textEntity.append("--");            textEntity.append(BOUNDARY);            textEntity.append("\r\n");            textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");            textEntity.append(entry.getValue());            textEntity.append("\r\n");        }        //计算传输给服务器的实体数据总长度        int dataLength = textEntity.toString().getBytes().length + fileDataLength +  endline.getBytes().length;                URL url = new URL(path);        int port = url.getPort()==-1 ? 80 : url.getPort();        Socket socket = new Socket(InetAddress.getByName(url.getHost()), port);               OutputStream outStream = socket.getOutputStream();        //下面完成HTTP请求头的发送        String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n";        outStream.write(requestmethod.getBytes());        String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";        outStream.write(accept.getBytes());        String language = "Accept-Language: zh-CN\r\n";        outStream.write(language.getBytes());        String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n";        outStream.write(contenttype.getBytes());        String contentlength = "Content-Length: "+ dataLength + "\r\n";        outStream.write(contentlength.getBytes());        String alive = "Connection: Keep-Alive\r\n";        outStream.write(alive.getBytes());        String host = "Host: "+ url.getHost() +":"+ port +"\r\n";        outStream.write(host.getBytes());        //写完HTTP请求头后根据HTTP协议再写一个回车换行        outStream.write("\r\n".getBytes());        //把所有文本类型的实体数据发送出来        outStream.write(textEntity.toString().getBytes());               //把所有文件类型的实体数据发送出来        for(FormFile uploadFile : files){        StringBuilder fileEntity = new StringBuilder();         fileEntity.append("--");         fileEntity.append(BOUNDARY);         fileEntity.append("\r\n");         fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");         fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");         outStream.write(fileEntity.toString().getBytes());         if(uploadFile.getInStream()!=null){         byte[] buffer = new byte[1024];         int len = 0;         while((len = uploadFile.getInStream().read(buffer, 0, 1024))!=-1){         outStream.write(buffer, 0, len);         }         uploadFile.getInStream().close();         }else{         outStream.write(uploadFile.getData(), 0, uploadFile.getData().length);         }         outStream.write("\r\n".getBytes());        }        //下面发送数据结束标志,表示数据已经结束        outStream.write(endline.getBytes());                BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));        if(reader.readLine().indexOf("200")==-1){//读取web服务器返回的数据,判断请求码是否为200,如果不是200,代表请求失败        return false;        }        outStream.flush();        outStream.close();        reader.close();        socket.close();        return true;}

9、采用异步框架async-http中的AsyncHttpClient执行post、get操作,需要下载相应的jar包,下载地址:https://github.com/loopj/android-async-http,它是对HttpClient的一个封装,这么我们不必考虑是否在主线程更新UI问题了,也不用写很多的代码就可以简单的实现客户端向服务器提交数据、文件等信息

发送一个get请求:

AsyncHttpClient client = new AsyncHttpClient();client.get("http://www.google.com", new AsyncHttpResponseHandler() {    @Override    public void onStart() {        // called before request is started    }    @Override    public void onSuccess(int statusCode, Header[] headers, byte[] response) {        // called when response HTTP status is "200 OK"    }    @Override    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {        // called when response HTTP status is "4XX" (eg. 401, 403, 404)    }    @Override    public void onRetry(int retryNo) {        // called when request is retried}});

发送get、post请求:

import com.loopj.android.http.*;public class TwitterRestClient {  private static final String BASE_URL = "http://api.twitter.com/1/";  private static AsyncHttpClient client = new AsyncHttpClient();  public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {      client.get(getAbsoluteUrl(url), params, responseHandler);  }  public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {      client.post(getAbsoluteUrl(url), params, responseHandler);  }  private static String getAbsoluteUrl(String relativeUrl) {      return BASE_URL + relativeUrl;  }}

向服务器发送json数据:

 public void getPublicTimeline() throws JSONException {        TwitterRestClient.get("statuses/public_timeline.json", null, new JsonHttpResponseHandler() {            @Override            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {                // If the response is JSONObject instead of expected JSONArray            }                        @Override            public void onSuccess(int statusCode, Header[] headers, JSONArray timeline) {                // Pull out the first event on the public timeline                JSONObject firstEvent = timeline.get(0);                String tweetText = firstEvent.getString("text");                // Do something with the response                System.out.println(tweetText);            }        });    }
简单参数携带:

RequestParams params = new RequestParams();params.put("key", "value");params.put("more", "data");

文件流参数携带:

InputStream myInputStream = blah;RequestParams params = new RequestParams();params.put("secret_passwords", myInputStream, "passwords.txt");
文件参数携带:

File myFile = new File("/path/to/file.png");RequestParams params = new RequestParams();try {    params.put("profile_picture", myFile);} catch(FileNotFoundException e) {}
字节数据参数携带:

byte[] myByteArray = blah;RequestParams params = new RequestParams();params.put("soundtrack", new ByteArrayInputStream(myByteArray), "she-wolf.mp3");
从服务器下载二进制数据:

AsyncHttpClient client = new AsyncHttpClient();client.get("http://example.com/file.png", new FileAsyncHttpResponseHandler(/* Context */ this) {    @Override    public void onSuccess(int statusCode, Header[] headers, File response) {        // Do something with the file `response`    }});
这个框架里面还有很多实用方法供我们使用,大家可以下载下来自己进行学习,建议使用这个框架对android中的网络进行操作,方便简洁!

大家可以将代码复制下来看看,掌握好了以后就直接可以拿来用咯!

更多相关文章

  1. Android仿人人客户端(v5.7.1)——网络模块处理的架构
  2. Android(安卓)RxJava:细说 线程控制(切换 / 调度 )(含Retrofit实例讲
  3. 如何配置ubuntu服务器用于embedded-linux的开发
  4. Android数据存储(五) SQLite数据库在Android中的API
  5. 优化Recorder H5录音:可边录边转码上传服务器,支持微信提供Android
  6. 【Android(安卓)开发】:UI控件之 Gallery 画廊控件的使用
  7. android aidl 进程间通信需要注意msg的大小(android.os.Transacti
  8. Android之数据库Greedao的使用
  9. android通过webview+jquery设计界面

随机推荐

  1. Android之WebView篇
  2. Android 网络显示图片
  3. Android中的dispatchTouchEvent()、onInt
  4. Android关机界面代码
  5. Android签名制作apk
  6. Failure [INSTALL_FAILED_TEST_ONLY: ins
  7. 个人帐号密码信息管理(PAPIM)软件构想
  8. apk分享: Android应用更换皮肤功能的实现
  9. android volatile的使用
  10. android > android 发布各大市场