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();
相关新闻>>
- 发表评论
-
- 最新评论 更多>>