android api json Parser

时间:2017-12-05 16:07:01

标签: android json nullpointerexception jsonparser

嗨我有一个应用程序显示来自Twitch.tv的在线流媒体,数据来自我发送的请求,但我无法解析他们这是我的代码

 public ArrayList<Twitch> ParseTwitch(JSONArray object, Context context){
ArrayList<Twitch> chanels = new ArrayList<>();
try {
    for (int i = 0;i<object.length();i++){
        JSONObject streamObjct = object.getJSONObject(i);
        JSONObject chanel = streamObjct.getJSONObject("channel");
        JSONObject url= streamObjct.getJSONObject("preview");
        final Twitch twitch = new Twitch();
        twitch.setName(chanel.getString(CHANEL_NAME));
        twitch.setStatus(chanel.getString(CHANEL_STATUS));
        twitch.setGame(streamObjct.getString(GAME));
        twitch.setLanguage(chanel.getString(BROADCASTER_LANGUAGE));
        twitch.setViewrs(streamObjct.getInt(VIEWRS));
        String imageUrl = url.getString("medium");
        ImageRequest request  =new ImageRequest(imageUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                twitch.setLogo(response);
            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue quew = Volley.newRequestQueue(context);
        quew.add(request);
        chanels.add(twitch);
    }
    return chanels;
}catch (JSONException e){
    e.printStackTrace();
}

return chanels;
}

这是我的要求 https://api.twitch.tv/kraken/search/streams?query=poker&client_id=gxdzx71jhztgyypn4kjemm1htovv1o

至少有人能告诉我应该使用sqlite数据库吗?

我称之为TwitchParcer

 private void prepareData(String url, final ListView mListView) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray stream = response.getJSONArray("streams");
                TwitchApiParser parser = new TwitchApiParser();
                ArrayList items= parser.ParseTwitch(stream);
                TwitchAdapter adapter = new TwitchAdapter(getContext(),
                        R.layout.activity_last_news_fragment,items);
                mListView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getContext(), "Ridi Bro", Toast.LENGTH_SHORT).show();
        }
    });
    RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
    quew.add(request);
}

这是新的错误

Process: app.mma.introsliderproject, PID: 11542
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                            at app.mma.PokerInfo.twitch.TwitchAdapter.getView(TwitchAdapter.java:39)

1 个答案:

答案 0 :(得分:1)

我解决了这个问题,这是完整的代码

public class TwitchFragment extends Fragment {



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.twitch_main, container, false);
    String url="http://192.168.1.101/mySite/Flowers/flowers2.json";
    ListView mListView = (ListView) view.findViewById(R.id.twitch_last);
    prepareData(url,mListView);
    return view;
}
private void prepareData(String url, final ListView mListView) {

    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray stream = response.getJSONArray("streams");
                TwitchApiParser parser = new TwitchApiParser();
                ArrayList items= parser.ParseTwitch(stream,getActivity().getApplicationContext());
                TwitchAdapter adapter = new TwitchAdapter(getContext(),
                        R.layout.twitch,items);
                mListView.setAdapter(adapter);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getContext(), "Ridi Bro", Toast.LENGTH_SHORT).show();
        }
    });
    RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
    quew.add(request);
}

和解析器

public class TwitchApiParser {
private final String CHANEL_NAME="display_name";
private final String GAME="game";
private final String CHANEL_STATUS = "status";
private final String BROADCASTER_LANGUAGE = "broadcaster_language";
private final String VIEWRS = "viewers";
public ArrayList<Twitch> ParseTwitch(JSONArray object, Context context){
ArrayList<Twitch> chanels = new ArrayList<>();
try {
    for (int i = 0;i<object.length();i++){
        JSONObject streamObjct = object.getJSONObject(i);
        JSONObject chanel = streamObjct.getJSONObject("channel");
        JSONObject url= streamObjct.getJSONObject("preview");
        final Twitch twitch = new Twitch();
        twitch.setName(chanel.getString(CHANEL_NAME));
        twitch.setStatus(chanel.getString(CHANEL_STATUS));
        twitch.setGame(streamObjct.getString(GAME));
        twitch.setLanguage(chanel.getString(BROADCASTER_LANGUAGE));
        twitch.setViewrs(streamObjct.getInt(VIEWRS));
        String imageUrl = url.getString("medium");
        ImageRequest request  =new ImageRequest(imageUrl, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                twitch.setLogo(response);
            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        RequestQueue quew = Volley.newRequestQueue(context);
        quew.add(request);
        chanels.add(twitch);
    }
    return chanels;
}catch (JSONException e){
    e.printStackTrace();
}

return chanels;
}