I am fairly new to android and java. I am trying to add JSON data to a listview. I have the JSON data coming in and printing to log, but I am having a hard time getting the data to the list view. I've tried and arraylist on row numbers and it works, but don't know how to get the JSON data there. Any pointers or advice would be appericated.
I am looking for a result Like
(Keys)ID Name Tourn_ID (Values) 1 Tournamnet1 MI2016 (Values) 2 Tournamnet2 UT2016 (Values) 3 Tournamnet4 USNC2016 etc.
Here is my code Java.
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import static android.R.*;
import static android.R.layout.simple_list_item_1;
import static java.util.Arrays.asList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DownloadTask task = new DownloadTask();
task.execute("http://www.goalballscoreboard.net/mobile/downloads/WebServices/Tournnames/Tournnames.php?rows=all");
ListView tournListView = (ListView) findViewById(R.id.tournListView);
final ArrayList<String> myTournList = new ArrayList<String>(asList("ROW 1", "ROW 2", "Row3"));
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, simple_list_item_1, myTournList);
tournListView.setAdapter(arrayAdapter);
}
public class DownloadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1) {
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONObject jsonObject = new JSONObject(result);
String tournInfo = jsonObject.getString("posts");
Log.i("Tourn INFO", tournInfo);
JSONArray arr = new JSONArray(tournInfo);
List<String> list = new ArrayList<String>();
for (int i = 0; i < arr.length(); i++) {
JSONObject jsonPart = arr.getJSONObject(i);
JSONObject jsonObjectPost = jsonPart.getJSONObject("post");
//Log.i("Each Tournament Object", jsonPart.getString("post"));
Log.i("ID", jsonObjectPost.getString("ID"));
Log.i("Name of Tournament", jsonObjectPost.getString("NAME"));
Log.i("TOURN_ID", jsonObjectPost.getString("TOURN_ID"));
String id = jsonObjectPost.get("ID").toString();
String name = jsonObjectPost.get("NAME").toString();
String tournID = jsonObjectPost.get("TOURN_ID").toString();
list.add(jsonObjectPost.getString("ID") + ", " + jsonObjectPost.getString("NAME") + "," + jsonObjectPost.getString("TOURN_ID") + "\n");
}
System.out.println("" + list.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}