这是我的代码。这只是整个应用程序的一小部分。它发出HTTP请求并从septa获取列车数据。代码是正确的,因为它在java项目中工作。请注意,当我尝试在单独的java类中执行简单的http请求部分并将其作为java文件运行时。在运行中,它给了我一个致命错误:java.lang.String的无效布局值。我试着在这个论坛上查看帖子。什么都行不通。不知道为什么会这样。代码在以前工作了!
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()
{
}
}
}
答案 0 :(得分:0)
您应该通过将AsyncTask定义为<Intent, Void, Void>
来避免可能的线程问题,并调用其执行方法,例如trainSchedule.execute(getIntent());
将doInBackground(Void... params)
更改为doInBackground(Intent... params)
并从之前获取该意图的额外内容您使用
String start = params[0].getExtras().getString("fromStation");
String end = params[0].getExtras().getString("toStation");
答案 1 :(得分:0)
使用URLEncoder encode方法对您的网址进行编码,这会将空格更改为%20
以及其他必要的编码
正如所指出的,上面的方法实际上会将空格编码为+
,这在这种情况下并不好,因为服务器不能很好地处理它们。您也可以尝试Uri.encode,看起来它应该像你想要的那样将空格编码为%20。
根据this answer,您必须在使用URLEncoder
之后自行对空格进行编码,如下所示:URLEncoder.encode(sURL, "UTF-8").replace("+", "%20")
答案 2 :(得分:0)
将其作为java文件运行。在运行中它给我一个致命错误:java.lang.String的值无效布局值为
将其作为Android应用程序运行,而不是作为通用Java应用程序运行。