I'm creating an App, where it will display some information from an external database using a JSON file so that I can display this information in the App. But when I run the code, the list in my app returns null and void. I would like to know where I am failing to build my Code.
public class PontosActivity extends AppCompatActivity {
private String TAG = PontosActivity.class.getSimpleName();
private ListView lstPontos;
private static String url = "http://mpeprojeto.com/pontos";
ArrayList<HashMap<String, String>> pontosList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pontos);
pontosList = new ArrayList<>();
lstPontos = (ListView) findViewById(R.id.listPontos);
new GetPontos().execute();
}
private class GetPontos extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
// Making a request to url and getting response
String jsonStr = sh.makeHttpHandler(url);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
JSONArray pontos = jsonObj.getJSONArray("pontos");
// looping through All Contacts
for (int i = 0; i < pontos.length(); i++) {
JSONObject c = pontos.getJSONObject(i);
String data = c.getString("datar");
String hora = c.getString("horar");
String lat = c.getString("latr");
String longt = c.getString("longtr");
// tmp hash map for single contact
HashMap<String, String> ponto = new HashMap<>();
// adding each child node to HashMap key => value
ponto.put("datar", data);
ponto.put("horar", hora);
ponto.put("latr", lat);
ponto.put("longtr", longt);
// adding contact to contact list
pontosList.add(ponto);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error1: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error2: " + e.getMessage(),
Toast.LENGTH_LONG)
.show();
}
});
}
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Avalie novamente seus ultimos pontos, pois não foi encontrado nenhum!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if(result!=null) {
ListAdapter adapter = new SimpleAdapter(
PontosActivity.this, pontosList,
R.layout.activity_listpontos, new String[]{"Data:", "Hora:",
"Latitude:", "Longitude:"}, new int[]{R.id.dataPonto,
R.id.horaPonto, R.id.latitudePonto, R.id.longitudePonto});
adapter.notifyAll();
lstPontos.setAdapter(adapter);
}
else
{
Toast.makeText(getApplicationContext(), "Nenhum Ponto Registrado", Toast.LENGTH_LONG).show();
}
}
}
JSON file:
{
"pontos": [
{
"datar": "14/11/2016",
"horar": "20:37:001",
"latr": "-22.778899",
"longtr": "-47.554477"
},
{
"datar": "14/11/2016",
"horar": "20:39:001",
"latr": "-22.748596",
"longtr": "-47.858532"
}
]
}
HttpHandler:
public class HttpHandler {
private static final String TAG = HttpHandler.class.getSimpleName();
public String makeHttpHandler(String reqUrl){
String resposta = null;
try{
URL url = new URL(reqUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream is = new BufferedInputStream(connection.getInputStream());
resposta = convertStreamToString(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resposta;
}
XML:
<TextView
android:id="@+id/dataPonto"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="" />
<TextView
android:id="@+id/horaPonto"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=""
android:layout_alignParentTop="true" />
<TextView
android:id="@+id/latitudePonto"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="" />
<TextView
android:id="@+id/longitudePonto"
android:layout_width="67dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text=""/>