zoukankan      html  css  js  c++  java
  • Android—网络请求

    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.List;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;
    
    /**
     * Connect to the internet
     * 
     * @author crane
     * 
     */
    public class HttpTools {
        /**
         * Post data to server
         * 
         * @param sUrl
         * @param entity
         * @return
         */
        public static String postData(String sUrl, MultipartEntity entity) {
            String destUrl = "";
            destUrl = sUrl;
            String sResult = "";
            // instantiate HttpPost object from the url address
            HttpEntityEnclosingRequestBase httpRequest = new HttpPost(destUrl);
            try {
                httpRequest.setEntity(entity);
                // execute the post and get the response from servers
                HttpResponse httpResponse = new DefaultHttpClient()
                        .execute(httpRequest);
                int code = httpResponse.getStatusLine().getStatusCode();
                if (httpResponse.getStatusLine().getStatusCode() == 200) {
                    // get the result
                    String strResult = EntityUtils.toString(httpResponse
                            .getEntity());
                    sResult = strResult;
                    System.out.println(strResult);
                } else {
                    System.out.println("Error Response"
                            + httpResponse.getStatusLine().toString());
                }
            } catch (Exception e) {
                System.out.println("error occurs");
            }
            return sResult;
        }
    
        /**
         * Get data from server
         * 
         * @param urlPath
         * @return
         */
        public static String getData(String sUrl) {
            String urlPath = sUrl;
            ByteArrayOutputStream outStream = null;
            String sResult = "";
            try {
                outStream = new ByteArrayOutputStream();
                byte[] data = new byte[1024];
                int len = 0;
                URL url = new URL(urlPath);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                InputStream inStream = conn.getInputStream();
                while ((len = inStream.read(data)) != -1) {
                    outStream.write(data, 0, len);
                }
                inStream.close();
                sResult = new String(outStream.toByteArray()).trim();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
            return sResult;
        }
    
        public static String post(String sUrl, List<NameValuePair> parameters) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(sUrl);
            String sResult = "";
    
            try {
                HttpEntity entity = new UrlEncodedFormEntity(parameters, HTTP.UTF_8); // 设置编码,防止中文乱码
                httpPost.setEntity(entity);
                // httpClient执行httpPost表单提交
                HttpResponse response = httpClient.execute(httpPost);
                // 得到服务器响应实体对象
                HttpEntity responseEntity = response.getEntity();
                if (responseEntity != null) {
                    sResult = EntityUtils.toString(responseEntity, "utf-8");
                    System.out.println("表单上传成功!");
                } else {
                    System.out.println("服务器无响应!");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                // 释放资源
                httpClient.getConnectionManager().shutdown();
            }
            return sResult.replace(",", ",");
        }
    
    }

    所需jar包:httpmime-4.1.3.jar
  • 相关阅读:
    程序员版狂人日记二 .
    [骑行小记1]骑在上海
    启动/关闭xp_cmdshell
    脑力风暴之小毛驴历险记(3)低进高出的小鸡(上)
    如何查看SQL Server的实例名(转载)
    MSBuild 项目属性以及任务参考
    SQL update 语句中使用表别名&&查找占用数据库的进程
    [当算法遇上数学]元芳,你怎么能随即生成m个数,让其和等于n?(加强版)
    IIS error: Service Unavailable : HTTP Error 503. The service is unavailable
    SQL with(nolock)详解 [转]
  • 原文地址:https://www.cnblogs.com/crane13/p/3688366.html
Copyright © 2011-2022 走看看