`

Android从服务器端下载数据

阅读更多
package com.seiosoft.mobileec.commons;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

/**
 * 从服务器端下载数据
 * 
 * @author yuanshouhui
 * 
 */
public class HttpDownload {

	public static String getJSONData(String url)
		throws ClientProtocolException, IOException {
		String result = "";
		HttpGet httpGet = new HttpGet(url);
		HttpClient httpClient = new DefaultHttpClient();
		HttpResponse httpResponse = null;

		try {
			httpResponse = httpClient.execute(httpGet);
			HttpEntity httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				InputStream inputStream = httpEntity.getContent();
				result = convertStreamToString(inputStream);

			}
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			throw e;
		} finally {
			httpClient.getConnectionManager().shutdown();
			httpResponse = null;
		}
		return result;

	}

	public static String convertStreamToString(InputStream is) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"),// 防止模拟器上的乱码
					512 * 1024);
		} catch (UnsupportedEncodingException e1) {

			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			Log.e("DataProvier convertStreamToString", e.getLocalizedMessage(),
					e);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

}

private static String json;
public void shareJson(int template_id, int Size, int Type, double maxValue,
			String oName) {
		// Start Insert 从服务端获取图片, 放入sd卡中

		String url = Commons.URL_EC_IMAGE_GET + "?template_id=" + template_id
				+ "&Size=" + Size + "&type=" + Type + "&ImageVersion="
				+ String.valueOf(maxValue);
		String fileName_old = oName + "_" + oType + "_" + oSize + "_"
				+ template_id + "_" + maxValue + ".png";
		String filePath_old = ALBUM_PATH + fileName_old;
		Log.i("url", url);
		try {
			json = HttpDownload.getJSONData(url);//***
			JSONObject root;
			try {
				root = new JSONObject(json.toString());
				String items = root.getString("ImageUrl");// 获取服务端json中数据
				Integer count = Integer.parseInt(root.getString("Count"));
				if (count == 0) {
					mBitmap = BitmapFactory.decodeFile(filePath_old);
				} else {
					Log.i("======= :", "" + items);
					String img_url = items;
					String fileName = img_url.substring(
							img_url.lastIndexOf('/') + 1, img_url.length());// 提取下载图片的文件名
					mBitmap = Commons.getBitmap(img_url);
					DeleteFile(filePath_old);
					saveFile(mBitmap, fileName);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics