直接看代码吧.
//1.这里获取图片
runOnUiThread(new Runnable() {
@ Override
public void run() {
SharedPreferences settings = getSharedPreferences("settings", 0);
String userId = settings.getString("userid","");
String imgType = "0"; //0正面 //1侧面
String result = YdHttpPostUtils.getImgPhotos(userId,imgType);
try {
if(StringUtils.isNotEmpty(result) && "200".equals(new JSONObject(result).getString("code"))){
JSONObject jsonObject = new JSONObject(result).getJSONObject("data");
String imgurl = jsonObject.getString("imgurl");
String img_ip ="#34;;
String imgUri = img_ip + "/" +imgurl;
// 侧面照片
ImageView imgView = (ImageView) findViewById(R.id.layout_mdd_frontphoto_img_face);
new DownloadImageTask(imgView).execute(imgUri);
}
} catch (JSON Exception e) {
e.printStackTrace();
}
}
}
);
可以看到先通过Shared Pre ferences,获取提前存的配置,userId,以及imgType,然后去请求后台获取图片的url数据:
public static String getImgPhotos(String userId,String imgType) {
// 请求url
String url = "#34;;
try {
Map<String, Object> map = new HashMap <>();
map.put("imgType",imgType);
map.put("userId",userId);
String param = GsonUtils.toJson(map);
String result = HttpUtil.post(url, "application/json", param);
//String result = HttpUtil.post(url, "application/x-www-form-urlencoded", param);
System.out.println(result);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
这里用到的工具类:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io. InputStream Reader;
import java .net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* http 工具类
*/public class HttpUtil {
public static String baidu post(String request Url, String accessToken, String params )
throws Exception {
String contentType = "application/x-www-form-urlencoded";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
//1.自动门店登录用
public static String post(String requestUrl, String params)
throws Exception {
String contentType = "application/x-www-form-urlencoded";
return HttpUtil.post(requestUrl, contentType, params);
}
public static String baidupost(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.baidupost(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, contentType, params, encoding);
}
public static String baidupost(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String post(String requestUrl, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> Header s = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
}
得到imgurl以后,把这个网络图片路径,传到:DownloadImageTask 这个类中
new DownloadImageTask(imgView).execute(imgUri);
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap > {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory. decode Stream(in);
} catch (Exception e) {
//Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
bmImage.setImageBitmap(bitmap);
bmImage.setVisibility(View.VISIBLE);
super.onPostExecute(bitmap);
}
}
这样就可以异步获取远程图片了.