Android 是否可以将Http.POST与通用映像加载程序一起使用
我正在使用尝试使用http POST而不是常规GET加载图像。POST请求将在正文中包含数据(键值对) 有没有一种方法可以使用通用图像加载器来实现这一点 到目前为止,我已经尝试为配置扩展BaseMageDownloader,但我认为我遗漏了一些其他内容:Android 是否可以将Http.POST与通用映像加载程序一起使用,android,universal-image-loader,Android,Universal Image Loader,我正在使用尝试使用http POST而不是常规GET加载图像。POST请求将在正文中包含数据(键值对) 有没有一种方法可以使用通用图像加载器来实现这一点 到目前为止,我已经尝试为配置扩展BaseMageDownloader,但我认为我遗漏了一些其他内容: public class SmartImageDownloader extends BaseImageDownloader { /** * Constructor * @param context */ public SmartImag
public class SmartImageDownloader extends BaseImageDownloader {
/**
* Constructor
* @param context
*/
public SmartImageDownloader(Context context) {
super(context);
}
@Override
public InputStream getStream(String imageUri, Object extra) throws IOException {
switch (Scheme.ofUri(imageUri)) {
case HTTP:
case HTTPS:
KLog.e(TAG, "getStream: " + extra.toString(), KLog.Severity.WTF);
HttpURLConnection conn = createConnection(imageUri, extra);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery((List<NameValuePair>)extra));
writer.flush();
writer.close();
os.close();
return new BufferedInputStream(conn.getInputStream(), BUFFER_SIZE);
case FILE:
return getStreamFromFile(imageUri, extra);
case CONTENT:
return getStreamFromContent(imageUri, extra);
case ASSETS:
return getStreamFromAssets(imageUri, extra);
case DRAWABLE:
return getStreamFromDrawable(imageUri, extra);
case UNKNOWN:
default:
return getStreamFromOtherSource(imageUri, extra);
}
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
}
其中:
public static final DisplayImageOptions DefaultDisplayImageOptions(List<NameValuePair> params){
return new DisplayImageOptions.Builder()
.imageDownloader(new SmartImageDownloader(this))
.resetViewBeforeLoading(true)
.cacheInMemory(true)
.cacheOnDisc(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.extraForDownloader(params)
.build();
}
公共静态最终显示图像选项DefaultDisplayImageOptions(列表参数){
返回新的DisplayImageOptions.Builder()
.imageDownloader(新的SmartImageDownloader(本))
.ResetViewBefore加载(真)
.cacheInMemory(真)
.cacheOnDisc(正确)
.imageScaleType(imageScaleType.IN\u示例\u电源\u,共2个)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.extraForDownloader(参数)
.build();
}
我所做的;我试图做的是,为了加载图像视图的图像,我构造了一个DisplayImageOptions,并将post参数作为额外参数。目前,它不断返回java.io.FileNotFoundException。谁能给我一个指针?谢谢 您是否在配置中设置了
SmartImageDownloader
。我在我的应用程序实例中初始化了它一次。我将UIL用于纯HTTP.GET-on图像,这次我只想通过POST请求获得它。看起来你做的一切都很好,但我不能说你为什么得到FileNotFoundException
。可能是您的URL错误,或参数错误。也许试着调试。@NOSTRA是的,我做了一些测试,HttpURLConnection上的部分似乎是使用url编码而不是我需要的表单数据类型来完成的。谢谢你的意见。我会努力的!
public static final DisplayImageOptions DefaultDisplayImageOptions(List<NameValuePair> params){
return new DisplayImageOptions.Builder()
.imageDownloader(new SmartImageDownloader(this))
.resetViewBeforeLoading(true)
.cacheInMemory(true)
.cacheOnDisc(true)
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.bitmapConfig(Bitmap.Config.ARGB_8888)
.extraForDownloader(params)
.build();
}