我想让这个链接“”工作。我想检索 RSS 提要并在文本视图中显示它,
我已经尝试了代码,但似乎没有工作。我猜代码有问题,但我不确定哪里出了问题。我是安卓新手。
我需要帮助,请。
谢谢。
这是我的代码。
MainActivity.java
public class MainActivity extends Activity {
TextView psi;
class MyWeather{
String title;
String description;
public String toString(){
return "\n- "
+ "Condition: " + title + "\n"
+ description +"\n";
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
psi = (TextView)findViewById(R.id.psi);
new MyAsyncTask().execute();
}
public class MyAsyncTask extends AsyncTask<Void, Void, Void> {
ProgressDialog dialog;
MyWeather weatherResult;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("Please wait...");
dialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
String weatherString = QueryYahooWeather();
Document weatherDoc = convertStringToDocument(weatherString);
weatherResult = parseWeather(weatherDoc);
Document dest = null;
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder parser;
try {
parser = dbFactory.newDocumentBuilder();
dest = parser.parse(new ByteArrayInputStream(src.getBytes()));
} catch (ParserConfigurationException e1) {
e1.printStackTrace();
Toast.makeText(MainActivity.this,
e1.toString(), Toast.LENGTH_LONG).show();
} catch (SAXException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
}
return dest;
}
private MyWeather parseWeather(Document weatherDoc) {
MyWeather myWeather = new MyWeather();
//<description>Yahoo! Weather for New York, NY</description>
//myWeather.description = srcDoc.getElementsByTagName("description")
//.item(0)
//.getTextContent();
Node locationNode = srcDoc.getElementsByTagName("item").item(0);
myWeather.title = locationNode.getAttributes()
.getNamedItem("title")
.getNodeValue()
.toString();
myWeather.description = locationNode.getAttributes()
.getNamedItem("description")
.getNodeValue()
.toString();
return myWeather;
}
}
}
private Document convertStringToDocument(String weatherString) {
// TODO Auto-generated method stub
return null;
}
private String QueryYahooWeather() {
// TODO Auto-generated method stub
String qResult = "";
String queryString = "app2.nea.gov.sg/data/rss/nea_psi.xml";
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(queryString);
try {
HttpEntity httpEntity = httpClient.execute(httpGet).getEntity();
if (httpEntity != null){
InputStream inputStream = httpEntity.getContent();
Reader in = new InputStreamReader(inputStream);
BufferedReader bufferedreader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String stringReadLine = null;
while ((stringReadLine = bufferedreader.readLine()) != null) {
stringBuilder.append(stringReadLine + "\n");
}
qResult = stringBuilder.toString();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this,
e.toString(), Toast.LENGTH_LONG).show();
}
return qResult;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
dialog.dismiss();
psi.setText(weatherResult.toString());
super.onPostExecute(result);
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<TextView
android:id="@+id/psi"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>