我对这个主题进行了很多研究,但没有任何线索..
我正在从 Web 服务下载图像,但我必须使用 URL 传递 post 参数才能仅下载特定图像..
即使我不知道图像的格式,但是在使用 AppTester 时,当我使用 URL 传递 post 参数值时,我得到的响应是 web 服务是“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());
请有人帮忙..我在这里很困惑