-1

嗨,我有一个应用程序显示来自 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)
4

1 回答 1

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;
}
于 2017-12-05T19:59:46.527 回答