当前位置:操作系统 > 安卓/Android >>

Android网络操作(上传下载等)

Java代码 
package com.maidong.utils; 
 
import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLConnection; 
import java.net.URLDecoder; 
import java.net.URLEncoder; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 
 
import org.apache.http.HttpEntity; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.protocol.HTTP; 
 
import android.app.Activity; 
import android.content.Context; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
 
public class InternetUtils { 
 
    private static final String USER_AGENT = "User-Agent"; 
 
    public static String httpPost(String url, List<NameValuePair> nameValuePairs) throws ClientProtocolException, IOException { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(url); 
        // List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
        // Your DATA 
        // nameValuePairs.add(new BasicNameValuePair("id", "12345")); 
        // nameValuePairs.add(new BasicNameValuePair("stringdata", 
        // "eoeAndroid.com is Cool!")); 
 
        httpPost.setHeader(USER_AGENT, "Mozilla/4.5"); 
        HttpEntity httpEntity = null; 
        try { 
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 
            httpEntity = httpclient.execute(httpPost).getEntity(); 
        } finally { 
            //httpPost.abort(); 
        } 
        return retrieveHttpEntity(httpEntity); 
    } 
 
    public static InputStream download(URL url) throws IOException { 
        URLConnection conn = url.openConnection(); 
        InputStream is = conn.getInputStream(); 
        return is; 
    } 
 
    public static byte[] downloadFileData(String surl) throws IOException { 
        URL url = new URL(surl); 
        URLConnection conn = url.openConnection(); 
        // 获取长度 
        int length = (int) conn.getContentLength(); 
        InputStream is = conn.getInputStream(); 
        byte[] imgData = null; 
        if (length != -1) { 
            imgData = new byte[length]; 
            byte[] temp = new byte[512]; 
            int readLen = 0; 
            int destPos = 0; 
            while ((readLen = is.read(temp)) > 0) { 
                System.arraycopy(temp, 0, imgData, destPos, readLen); 
                destPos += readLen; 
            } 
        } 
        return imgData; 
    } 
 
    public static InputStream download(String url) throws IOException { 
        return download(new URL(url)); 
    } 
 
    public static String httpPost(String url) throws ClientProtocolException, IOException { 
        return httpPost(url, new ArrayList<NameValuePair>()); 
    } 
 
    private static String retrieveHttpEntity(HttpEntity httpEntity) throws UnsupportedEncodingException, IllegalStateException, 
            IOException { 
        StringBuffer stringBuffer = new StringBuffer(); 
        InputStreamReader is = new InputStreamReader(httpEntity.getContent(), HTTP.UTF_8); 
        BufferedReader bufferedReader = new BufferedReader(is); 
        String line; 
        while ((line = bufferedReader.readLine()) != null) { 
            stringBuffer.append(line); 
        } 
        return stringBuffer.toString(); 
    } 
 
    public static String uploadFile(String actionUrl, String newName, InputStream fStream) { 
        String end = "\r\n"; 
        String twoHyphens = "--"; 
        String boundary = java.util.UUID.randomUUID().toString(); 
        DataOutputStream ds = null; 
        try { 
            URL url = new URL(actionUrl); 
        

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,