Android:使用Loopj和Gson

时间:2017-07-27 17:20:50

标签: android json gson loopj

尝试通过使用Loopj并使用GSON库来处理json来获取数据。但无法运行代码。我不知道我在哪里做错了。

这是我的LoopjClient.class

public class LoopjClient {
    AsyncHttpClient client;
    RequestParams requestParams;
    String jsonResponse;
    ActorsResponse response=null;
    public LoopjClient(){
        client=new AsyncHttpClient();
        requestParams=new RequestParams();
    }
    public void CallbackForGet(String url){
        client.get(url,new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {


                super.onSuccess(statusCode, headers, responseString);
              //  Toast.makeText(MainActivity.this, "this is success method of client", Toast.LENGTH_SHORT).show();
                Gson gson=new GsonBuilder().create();
                response=gson.fromJson(responseString,ActorsResponse.class);



            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
            }
        });

    }


    public List<ActorsResponse.ActorsBean> getData(){
        return response.getActors();
    }
}

这是我的MainActivity.class

public class MainActivity extends AppCompatActivity {

    ListView lv;
    List<ActorsResponse.ActorsBean> mList=null;
    String url="http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lv=(ListView)findViewById(R.id.actors_list);

        LoopjClient client=new LoopjClient();

        client.CallbackForGet(url);
        mList=client.getData();

        CustomActorAdapter adapter=new CustomActorAdapter(this,mList);
        lv.setAdapter(adapter);
    }
}

这是CustomAdapter.class

public class CustomAdapter extends BaseAdapter{

    List<ActorsResponse.ActorsBean> mList;
    Context context;
    LayoutInflater mInflater;

    public CustomActorAdapter(Context c, List<ActorsResponse.ActorsBean> object) {
        mList=object;
        context=c;
        mInflater=(LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {

        return mList.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder=null;
        if (view==null){
            holder=new ViewHolder();
            view=mInflater.inflate(R.layout.row,viewGroup,false);


            holder.name=(TextView)view.findViewById(R.id.actor_name);
            holder.dob=(TextView)view.findViewById(R.id.actor_dob);
            holder.spouse=(TextView)view.findViewById(R.id.actor_spouse);
            holder.height=(TextView)view.findViewById(R.id.actor_height);
            holder.children=(TextView)view.findViewById(R.id.actor_children);
            holder.nation=(TextView)view.findViewById(R.id.actor_country);
            holder.desc=(TextView)view.findViewById(R.id.actor_desc);
            holder.image=(ImageView)view.findViewById(R.id.actor_image);

            view.setTag(holder);

        }
        else
        {
            holder=(ViewHolder)view.getTag();
            holder.image.setImageDrawable(null);
        }

        String namu="Name: "+mList.get(i).getName();
        String dobu="DOB: "+mList.get(i).getDob();
        String spouseu="Spouse: "+mList.get(i).getSpouse();
        String childrenu="Children: "+mList.get(i).getChildren();
        String countryu="Country: "+mList.get(i).getCountry();
        String heightu="Height: "+mList.get(i).getHeight();
        String descu="DESCRIPTION : "+mList.get(i).getDescription();
        String image=mList.get(i).getImage();


        new DownloadImageTask(holder.image).execute(image);
        holder.name.setText(namu);
        holder.height.setText(heightu);
        holder.nation.setText(countryu);
        holder.dob.setText(dobu);
        holder.spouse.setText(spouseu);
        holder.children.setText(childrenu);
        holder.desc.setText(descu);


        return view;
    }



    static class ViewHolder{

        TextView name;
        TextView dob;
        TextView spouse;
        TextView children;
        TextView height;
        TextView nation;
        TextView desc;
        ImageView image;

    }


    public class DownloadImageTask extends AsyncTask<String,Void,Bitmap>{
        ImageView img;

        public DownloadImageTask(ImageView view){
            this.img=view;
        }

        @Override
        protected Bitmap doInBackground(String... strings) {
            Bitmap pic=null;
            String urldisplay=strings[0];
            InputStream in= null;
            try {
                in = new URL(urldisplay).openStream();
                pic=BitmapFactory.decodeStream(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return pic;

        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            img.setImageBitmap(bitmap);
        }
    }
}

还有最后一个类ActorsResponse.class,它是json格式的数据模型。

这是ActorsResponse.class

public class ActorsResponse {


    private List<ActorsBean> actors;

    public static ActorsResponse objectFromData(String str) {

        return new com.google.gson.Gson().fromJson(str, ActorsResponse.class);
    }

    public static ActorsResponse objectFromData(String str, String key) {

        try {
            JSONObject jsonObject = new JSONObject(str);

            return new com.google.gson.Gson().fromJson(jsonObject.getString(str), ActorsResponse.class);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static List<ActorsResponse> arrayActorsResponseFromData(String str) {

        Type listType = new com.google.gson.reflect.TypeToken<ArrayList<ActorsResponse>>() {
        }.getType();

        return new com.google.gson.Gson().fromJson(str, listType);
    }

    public static List<ActorsResponse> arrayActorsResponseFromData(String str, String key) {

        try {
            JSONObject jsonObject = new JSONObject(str);
            Type listType = new com.google.gson.reflect.TypeToken<ArrayList<ActorsResponse>>() {
            }.getType();

            return new com.google.gson.Gson().fromJson(jsonObject.getString(str), listType);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        return new ArrayList();


    }

    public List<ActorsBean> getActors() {
        return actors;
    }

    public void setActors(List<ActorsBean> actors) {
        this.actors = actors;
    }

    public static class ActorsBean {
        /**
         * name : Brad Pitt
         * description : William Bradley 'Brad' Pitt is an American actor and film producer. He has received a Golden Globe Award, a Screen Actors Guild Award, and three Academy Award nominations in acting categories
         * dob : December 18, 1963
         * country : United States
         * height : 1.80 m
         * spouse : Jennifer Aniston
         * children : Shiloh Nouvel Jolie-Pitt, Maddox Chivan Jolie-Pitt
         * image : http://microblogging.wingnity.com/JSONParsingTutorial/brad.jpg
         */

        private String name;
        private String description;
        private String dob;
        private String country;
        private String height;
        private String spouse;
        private String children;
        private String image;

        public static ActorsBean objectFromData(String str) {

            return new com.google.gson.Gson().fromJson(str, ActorsBean.class);
        }

        public static ActorsBean objectFromData(String str, String key) {

            try {
                JSONObject jsonObject = new JSONObject(str);

                return new com.google.gson.Gson().fromJson(jsonObject.getString(str), ActorsBean.class);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        public static List<ActorsBean> arrayActorsBeanFromData(String str) {

            Type listType = new com.google.gson.reflect.TypeToken<ArrayList<ActorsBean>>() {
            }.getType();

            return new com.google.gson.Gson().fromJson(str, listType);
        }

        public static List<ActorsBean> arrayActorsBeanFromData(String str, String key) {

            try {
                JSONObject jsonObject = new JSONObject(str);
                Type listType = new com.google.gson.reflect.TypeToken<ArrayList<ActorsBean>>() {
                }.getType();

                return new com.google.gson.Gson().fromJson(jsonObject.getString(str), listType);

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return new ArrayList();


        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getDescription() {
            return description;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public String getDob() {
            return dob;
        }

        public void setDob(String dob) {
            this.dob = dob;
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getHeight() {
            return height;
        }

        public void setHeight(String height) {
            this.height = height;
        }

        public String getSpouse() {
            return spouse;
        }

        public void setSpouse(String spouse) {
            this.spouse = spouse;
        }

        public String getChildren() {
            return children;
        }

        public void setChildren(String children) {
            this.children = children;
        }

        public String getImage() {
            return image;
        }

        public void setImage(String image) {
            this.image = image;
        }
    }
}

它只是给出错误, mList 是CustomAdapter.class中的空对象。我是朝正确的方向做的吗? 真的需要帮助!!!

0 个答案:

没有答案