1

我对这个主题进行了很多研究,但没有任何线索..

我正在从 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());

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

4

2 回答 2

1

试试这个代码

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);
于 2012-04-20T07:14:54.450 回答
0

最后我自己得到了答案:

我已经完成了编码:

    public InputStream HTTPImage(String uri1,List<NameValuePair> list) throws Exception
{
    InputStream input=null;
try {
    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpPost httpPost = new HttpPost(uri1);
    Log.i("postParameter,list", ""+list);
    if(list!=null)
    {

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

    }

    HttpResponse httpResponse = httpClient.execute(httpPost);
    input=(InputStream) httpResponse.getEntity().getContent();

        return input;
     }

在另一堂课中:我正在使用它:

    InputStream in=null;
        in=con2.HTTPImage("https://hd.picbusiness.com/icmdev/hhw/App/", postParameters2);
        Bitmap bMap=BitmapFactory.decodeStream(in);
        img.setImageBitmap(bMap);
        Log.i("bMap", ""+bMap);

谢谢大家的支持:)

于 2012-04-20T09:53:27.200 回答