gpt4 book ai didi

android - 从 Web 服务向 ImageView 显示图像(使用 URL 传递 post 参数)

转载 作者:行者123 更新时间:2023-11-30 04:14:36 26 4
gpt4 key购买 nike

我对这个话题有很多研究,但没有头绪..

我正在从 Web 服务下载图像,但我必须通过 URL 传递 post 参数才能仅下载特定图像..

即使我不知道图像的格式,但在使用 AppTester 时,当我通过 URL 传递后参数值时,我通过网络服务获得的响应是​​“image.png”

我在这里尝试的代码是:

    public String HTTPConnect(String uri1,List<NameValuePair> list,Context context)
{

try {
DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(uri1);
if(list!=null)
{

UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list);
httpPost.setEntity(formEntity);

}
//URI uri=httpPost.getURI();
HttpResponse httpResponse = httpClient.execute(httpPost);
// Log.i("RESPONSE RETURNS THIS :", ""+httpResponse);
// Log.i("getEntity().getContent() RETURNS THIS :", ""+httpResponse.getEntity().getContent());
in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
// String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line +" "); //sb.append(line +NL);
}
in.close();

result = sb.toString();

}
catch(UnsupportedEncodingException e)
{
String err = (e.getMessage()==null)?"Cant connect to server":e.getMessage();
Log.e("Network Error:",err);
}
catch (MalformedURLException e) {
String err = (e.getMessage()==null)?"Malformed Exception":e.getMessage();
Log.e("Malformed Exception:",err);

}
catch(Exception ex)
{
// Log.i("Exception,ex", ex.getMessage());
String err = (ex.getMessage()==null)?"NetworkConnectionException":ex.getMessage();
Log.e("NetworkConnectionException:",err);
}
finally {

if (in != null) {
try {
in.close();
} catch (Exception ex) {
String err = (ex.getMessage()==null)?"Excepion":ex.getMessage();
Log.e("Exception:",err);
}
}

}

return result;

}

在另一个类上,我调用此方法并将结果字符串转换为字节:

           ArrayList<NameValuePair> postParameters2 = new ArrayList<NameValuePair>();

postParameters2.add(new BasicNameValuePair("Token", "token"));
postParameters2.add(new BasicNameValuePair("Action", "GetThumb"));

Bitmap bMap=null;
String CustomerImgXml=HTTPConnect("URL", postParameters2, this);
bMap=BitmapFactory.decodeByteArray(CustomerImgXml.getBytes(), 0, CustomerImgXml.length());

请有人帮忙..我在这里很困惑

最佳答案

试试这段代码

Bitmap myBitmap; 
try {
url = new URL("http://upload.wikimedia.org/wikipedia/commons/thumb/0/01/Sachin_Tendulkar.jpg/250px-Sachin_Tendulkar.jpg");
connection = (HttpURLConnection) url
.openConnection();

connection.setDoInput(true);


connection.connect();
connection.setReadTimeout(120000);
InputStream input = connection.getInputStream();
myBitmap = BitmapFactory.decodeStream(input);

} catch (IOException e) {
e.printStackTrace();
return null;
}

ImageView.setImageBitmap(myBitmap);

关于android - 从 Web 服务向 ImageView 显示图像(使用 URL 传递 post 参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10241753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com