android json解析和图像加载

时间:2016-02-24 06:43:04

标签: android json

我想通过json的url解析数据和图像。

我使用以下代码来做同样的事情。但是当我向下滚动时,我不明白为什么图像会消失。

任何人都可以告诉我当我向下滚动时如何停止消失图像?

图像随机加载我该如何解决?

请在下面找到源代码链接: -

http://www.wingnity.com/blog/android-json-parsing-and-image-loading-tutorial/

公共类MainActivity扩展了Activity {

ArrayList<Actors> actorsList;

ActorAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    actorsList = new ArrayList<Actors>();
    new JSONAsyncTask().execute("http://milagro.in/wip/apps/n/THDC2.json");

    ListView listview = (ListView)findViewById(R.id.list);
    adapter = new ActorAdapter(getApplicationContext(), R.layout.row, actorsList);

    listview.setAdapter(adapter);

    listview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                long id) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), actorsList.get(position).gettata_project_name(), Toast.LENGTH_LONG).show();
        }
    });
}


class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

    ProgressDialog dialog;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        dialog = new ProgressDialog(MainActivity.this);
        dialog.setMessage("Loading, please wait");
        dialog.setTitle("Connecting server");
        dialog.show();
        dialog.setCancelable(false);
    }

    @Override
    protected Boolean doInBackground(String... urls) {
        try {

            //------------------>>
            HttpGet httppost = new HttpGet(urls[0]);
            HttpClient httpclient = new DefaultHttpClient();
            HttpResponse response = httpclient.execute(httppost);

            // StatusLine stat = response.getStatusLine();
            int status = response.getStatusLine().getStatusCode();

            if (status == 200) {
                HttpEntity entity = response.getEntity();
                String data = EntityUtils.toString(entity);


                JSONObject jsono = new JSONObject(data);
                JSONArray jarray = jsono.getJSONArray("data");

                for (int i = 0; i < jarray.length(); i++) {
                    JSONObject object = jarray.getJSONObject(i);

                    Actors actor = new Actors();

                    actor.settata_project_name(object.getString("tata_project_name"));
                    actor.setproject_Typology(object.getString("project_Typology"));
                    actor.setproject_logo_url(object.getString("project_logo_url"));
                    actor.setprice(object.getString("price"));



                    actorsList.add(actor);
                }
                return true;
            }

            //------------------>>

        } catch (ParseException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    protected void onPostExecute(Boolean result) {
        dialog.cancel();
        adapter.notifyDataSetChanged();
        if(result == false)
            Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

    }
}

}

public class ActorAdapter extends ArrayAdapter<Actors> {
ArrayList<Actors> actorList;
LayoutInflater vi;
int Resource;
ViewHolder holder;

public ActorAdapter(Context context, int resource, ArrayList<Actors> objects) {
    super(context, resource, objects);
    vi = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Resource = resource;
    actorList = objects;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // convert view = design
    View v = convertView;
    if (v == null) {
        holder = new ViewHolder();
        v = vi.inflate(Resource, null);
        holder.imageview = (ImageView) v.findViewById(R.id.ivImage);
        holder.Projectname = (TextView) v.findViewById(R.id.Projectname);
        holder.typology = (TextView) v.findViewById(R.id.typology);
        holder.price = (TextView) v.findViewById(R.id.price);

        v.setTag(holder);
    } else {
        holder = (ViewHolder) v.getTag();
    }
    new DownloadImageTask(holder.imageview).execute(actorList.get(position).getproject_logo_url());
    holder.imageview.setImageResource(R.drawable.ic_launcher);
    //new DownloadImageTask(holder.imageview).execute(actorList.get(position).getproject_logo_url());
    holder.Projectname.setText(actorList.get(position).gettata_project_name());
    holder.typology.setText(actorList.get(position).getproject_Typology());
    holder.price.setText("Price: " + actorList.get(position).getprice());

    return v;

}

static class ViewHolder {
    public ImageView imageview;
    public TextView Projectname;
    public TextView typology;
    public TextView price;

}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }

}

公共类演员{

private String tata_project_name;
private String project_Typology;
private String project_logo_url;
private String price;



public Actors() {
    // TODO Auto-generated constructor stub
}

public Actors(String tata_project_name, String project_Typology,String project_logo_url, String price ) {
    super();
    this.tata_project_name = tata_project_name;
    this.project_Typology = project_Typology;
    this.project_logo_url = project_logo_url;
    this.price = price;


}


public String gettata_project_name() {
    return tata_project_name;
}

public void settata_project_name(String tata_project_name) {
    this.tata_project_name = tata_project_name;
}

public String getproject_Typology() {
    return project_Typology;
}

public void setproject_Typology(String project_Typology) {
    this.project_Typology = project_Typology;
}

public String getproject_logo_url() {
    return project_logo_url;
}

public void setproject_logo_url(String project_logo_url) {
    this.project_logo_url = project_logo_url;
}

public String getprice() {
    return price;
}

public void setprice(String price) {
    this.price = price;
}

}

2 个答案:

答案 0 :(得分:1)

试试这个library Picaso 这是来自网址的最佳图片加载库。

答案 1 :(得分:0)

使用Volley库之后问题就解决了。