-1

I just can't seem to get it to work, I have tried many different things, the parsing works fine and returns a list of titles in an arraylist and I can't get them to then view on the ListView. Here is the code;

/**
 * Created by Matt on 4/21/2015.
 */
public class Discussion_Fragment extends Fragment {
View rootview;
ListView listView;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootview = inflater.inflate(R.layout.discussion_layout, container, false);
    ArrayList<String> titles = new ArrayList<String>();
    listView = (ListView) rootview.findViewById(R.id.listView1);
    parse(getHTTP("https://www.reddit.com/r/DotA2.json?limit=10"));

    for(int i=0;i<titles.size();i++){
        titles.add(titles.get(i));
    }

    ArrayAdapter saAdapter = new ArrayAdapter(getActivity(), R.layout.discussion_layout, titles);

    listView.setAdapter(saAdapter);

    return rootview;
}
public static String getHTTP(String urlToRead) {

    URL url;
    HttpURLConnection conn;
    BufferedReader rd;
    String line;
    String result = "";
    boolean response = false;
    while (!response)
    {
        try {
            url = new URL(urlToRead);
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = rd.readLine()) != null) {
                result += line;
            }
            rd.close();
            response = true;
        } catch (IOException e) {
            e.printStackTrace();
            try {
                Thread.sleep(600);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return result;
}
public static List<String> parse(String httpResponse)
{
    ArrayList<String> titles = new ArrayList<String>();
    try {
        JSONObject obj = new JSONObject(httpResponse);
        //JSONObject posts = obj.getJSONObject("kind");
        JSONObject posts1 = obj.getJSONObject("data");
        JSONArray posts2 = posts1.getJSONArray("children");


        for (int i = 0; i < posts2.length(); i++) {
            JSONObject test = posts2.getJSONObject(i);
            JSONObject test1 = test.getJSONObject("data");
            Object test2 = test1.get("title");
            titles.add(test2.toString());
        }
    }
    catch(Exception e) {
        e.printStackTrace();
    }
    return titles;
}
}
Maveňツ
  • 1
  • 12
  • 50
  • 89
mattz330
  • 59
  • 2
  • 13

3 Answers3

5

You should do like

List<String> titles = parse(getHTTP("https://www.reddit.com/r/DotA2.json?limit=10"));

As parse(...) method return List<String>

M D
  • 47,665
  • 9
  • 93
  • 114
  • Hi thanks, but I changed that and now the app just freezes/gets into an infinite loop when i click the discussions fragment and i can't tell what the error is... :s – mattz330 May 06 '15 at 04:58
  • @MatthewWren you can use this code in the [UIThread](http://stackoverflow.com/questions/3652560/what-is-the-android-uithread-ui-thread) – Maveňツ May 06 '15 at 05:03
  • 1
    @MatthewWren You should used `AsyncTask` for that. – M D May 06 '15 at 05:04
1

replace this:

parse(getHTTP("https://www.reddit.com/r/DotA2.json?limit=10"));

for(int i=0;i<titles.size();i++){
    titles.add(titles.get(i));
}

with this:

titles = parse(getHTTP("https://www.reddit.com/r/DotA2.json?limit=10"));
Mohib Irshad
  • 1,940
  • 23
  • 18
  • Hi thanks, but I changed that and now the app just freezes/gets into an infinite loop when i click the discussions fragment and i can't tell what the error is... :s – mattz330 May 06 '15 at 04:58
  • The reason is, you are fetching data from internet on UIThread which blocks i/o operations. Use asynctask: http://developer.android.com/reference/android/os/AsyncTask.html add the above line inside doInBackground function and other work inside onPostExecute – Mohib Irshad May 06 '15 at 06:13
0

Move your getHTTP() in AsynTask as you are fetching data on main thread. Do it in background.

AskMe09
  • 1
  • 2