Android使用JSON从web获取图像

Android使用JSON从web获取图像,android,json,Android,Json,我从网上得到新闻信息。所以我可以很容易地获取新闻的标题和主题,但问题是如何从json获取图像 这是我的密码: public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.haber_yapisi, nul

我从网上得到新闻信息。所以我可以很容易地获取新闻的标题和主题,但问题是如何从json获取图像

这是我的密码:

public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {  
                        convertView = mInflater.inflate(R.layout.haber_yapisi, null); // gets all of the views from adaptor_content and implements their id
                        holder = new ViewHolder();
                        holder.baslik = (TextView) convertView.findViewById(R.id.baslik);
                        holder.haber = (TextView) convertView.findViewById(R.id.haber);
                        holder.resim = (ImageView) convertView.findViewById(R.id.resim);
                        holder.aaa = (TextView) convertView.findViewById(R.id.aaa);
                        holder.bas=(TextView) findViewById(R.id.head_ana);
                        holder.bas.setText("Ana Sayfa");
                        convertView.setTag(holder);   
                      } else {
                        holder = (ViewHolder) convertView.getTag();
                      }

            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpget = new HttpGet("http://www.dha.com.tr/mobil/anasayfa.asp");
            HttpResponse response;

            try {
                response = httpclient.execute(httpget);
                HttpEntity entity = response.getEntity();
                if (entity != null) {

                 try {
                     InputStream instream = entity.getContent();
                     String line = "";
                     BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
                     StringBuilder json = new StringBuilder();

                    while ((line = reader.readLine()) != null) {
                           json.append(line + "\n");
                     }

                  StringBuilder stringBuilder = new StringBuilder();

                  JSONArray siteler = new JSONArray(json.toString());


                       JSONObject ss = siteler.getJSONObject(position);
                       holder.baslik.setText(ss.getString("strsubject"));
                       holder.haber.setText( ss.getString("strbody") );
                           holder.resim.setImageBitmap("WHAT SHOULD I DO FOR GETTİNG IMAGE")

                  holder.aaa.setText(stringBuilder.toString());

                   instream.close();

                } catch (Exception e) {
                           holder.aaa.setText("Error is: " + e);
                }
             }

           } catch (Exception e) {
                holder.aaa.setText("Error is : " + e);
           }


                        return convertView; 
            }
    }

请帮忙

你有json格式的图像url吗??如果是这样,则为此url创建另一个http连接,获取inputstream,然后从此流创建位图。
否则,请在此澄清json中图像的信息类型???

如果json包含可以使用的图像url

String imageBaseDirectory = "http://www.dha.com.tr/newpics/news/";
String imageName = "230620111119295717933.jpg";//get image name from json parsing;
    imageview.setImageURI(Uri.parse(imageBaseDirectory+imageName));

但是我可以看到您所引用的url不包含图像url,它只包含图像的名称

您的JSON包含文件名。假设您知道图像的路径,形成url并按照Shailendra的建议执行,例如:

URL url = new URL(imgBaseUrl + ss.getString("foto"));
URLConnection connection = url.openConnection();
FlushedInputStream fis = new FlushedInputStream(connection.getInputStream());
ByteArrayBuffer baf = new ByteArrayBuffer(100);
int current = 0;  
while((current = fis.read()) != -1){  
    baf.append((byte)current);  
}
fis.close();
holder.resim.setImageBitmap(BitmapFactory.decodeByteArray(baf, 0, baf.length()));
确保使用FlushedInputStream,如图所示

静态类FlushedInputStream扩展FilterInputStream{
公共FlushedInputStream(InputStream InputStream){
超级(输入流);
}
@凌驾
公共长跳过(长n)引发IOException{
长的总重量=0升;
while(totalBytesSkipped
大部分答案在这里的另一篇帖子中:

1) 创建url并将其传递给我提供的链接中的方法

String myPhoto = "foto2";
String url = "http://mysite.com/images/" + myPhoto + ".png";

如何从json加载图像?使用,它很容易:

String imageUrl = imgBaseUrl + ss.getString("foto"); // this is the image url
    Picasso.with(this.getActivity()).load(imageUrl).into(holder.resim); // holder.resim is the imageview

所有这些信息都在一个JsonObject中,照片的名称是“foto2”,所以如果这是一个文本,我应该执行holder.resim.settext(ss.getString(“foto”);但是它是一个图像,所以我能做什么呢?这是图像的url,所以当我写这段代码imageview.setImageURI(Uri.parse(');它不工作?因为不是图像文件的绝对路径。这里缺少图像的名称,您必须从json解析中获取该名称,并将该文件名附加到目录名,然后传递给UseTMageUri方法。现在见我的答案谢谢你提供的信息。。我尝试过这是一个很好的解决方案,但知道问题是没有图像出现吗?但代码并没有给出带有硬编码文件名的errortry,我使用字符串imageBaseDirectory=“”;字符串imageName=“23062011119295717933.jpg”;setImageURI(Uri.parse(imageBaseDirectory+imageName));我就是这样试的,但它没有给我显示图像!
String imageUrl = imgBaseUrl + ss.getString("foto"); // this is the image url
    Picasso.with(this.getActivity()).load(imageUrl).into(holder.resim); // holder.resim is the imageview