Here is my code. This is just a snippet of the whole application. It makes a HTTP request and gets train data from septa. The code is correct as it works in a java project. One note, when I try to do the simple http request part in a separate java class and run it as a java file. In the run it gives me a Fatal Error: Invalid Layout of java.lang.String at value. I tried looking at the thread at this forum. Nothing works. No idea why this is happening. The code was working before!
package com.cs275.septaassignment;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.HttpURLConnection;
import android.app.Activity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.google.gson.*;
import java.util.ArrayList;
public class TrainInfo extends Activity
{
protected static String startStation;
protected static String endStation;
private ListView listView;//ListView for the output
private ArrayAdapter<String> adapter;//Adapter to interact with the ListView
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.train_infolayout);
startStation = this.getIntent().getExtras().getString("fromStation");
endStation = this.getIntent().getExtras().getString("toStation");
ArrayList<String> trainInfo = new ArrayList<String>();
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, trainInfo);//initializing the ArrayAdapter
listView = (ListView)findViewById(R.id.train_listView);
listView.setAdapter(adapter);//Linking the adapter
adapter.setNotifyOnChange(true);//This ensures that the changes in data so the UI components can refresh themselves
TrainData trainSchedule = new TrainData();
trainSchedule.execute();
}
public class TrainData extends AsyncTask<Void,Void,Void>
{
ArrayList<String> nextTrains;
protected void onPostExecute(Void arg0)//This method is called after the background process is complete
{
adapter.clear();
for(int i = 0;i<nextTrains.size();i++)
{
System.out.println(nextTrains.get(i));
adapter.add(nextTrains.get(i));
}
adapter.add("Hello World");
TextView updateMessage = (TextView) findViewById(R.id.train_list_info);
updateMessage.setText("Here are the available trains for your trip from " + startStation + " to " + endStation);
adapter.notifyDataSetChanged();//This send the signal that the data is changes and allows the UI components like the ListView to get updated.
}
@Override
protected Void doInBackground(Void... params)
{
// TODO Auto-generated method stub
try
{
String sURL = "http://www3.septa.org/hackathon/NextToArrive/"+ startStation + "/" + endStation + "/10";
System.out.println(sURL);
URL url = new URL(sURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
JsonParser jp = new JsonParser();//Json parsing tool
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent()));//Getting content in Json
System.out.println("root:\n" + root);
JsonArray rootArray = root.getAsJsonArray();
nextTrains = new ArrayList<String>();
if(rootArray.size() > 0)
{
for(int i = 0;i<rootArray.size();i++)
{
JsonObject train = rootArray.get(i).getAsJsonObject();
String departureTime = train.get("orig_departure_time").getAsString();
String arrivalTime = train.get("orig_arrival_time").getAsString();
String delayTime = train.get("orig_delay").getAsString();
String trainNumber = train.get("orig_train").getAsString();
String result = "Train " + trainNumber + "\nDeparts from " + startStation + ": " + departureTime
+ "\nArrives at " + endStation + ": " + arrivalTime;
if(!delayTime.equals("On Time"))
result = result + "\nDelayed: " + delayTime;
nextTrains.add(result);
}
}else
{
nextTrains.add("No trains available from " + startStation + " to " + endStation);
}
}catch(Exception e)
{
System.out.println("Unable to make http request!");
}
return null;
}
public void retrieveData()
{
}
}
}