【发布时间】:2013-12-15 08:30:34
【问题描述】:
我想在我的 Android 设备上的文本框中查看新闻我已经获得了 xml 数据,但我无法解析我的数据以获取我的新闻
RSSFeedActivity 类
public class RSSFeedActivity extends Activity {
/** Called when the activity is first created. */
TextView a5bar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
a5bar = (TextView)findViewById(R.id.Data_feed);
String feed = "http://www.masrawy.com/News/rss/LocalPolitics.aspx";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(feed);
try {
HttpResponse response = client.execute(request);
StatusLine st_line = response.getStatusLine();
int statuescode = st_line.getStatusCode();
if(statuescode ==200){
InputStream jsonstream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(jsonstream));
StringBuilder builder = new StringBuilder();
String line;
while((line = reader.readLine())!=null){
builder.append(line);
}
String js_data = builder.toString();
a5bar.setText(js_data);
try{
JSONArray jArray = new JSONArray(js_data);
for(int i=0;i<jArray.length();i++)
{
JSONObject json_data = jArray.getJSONObject(i);
String id=json_data.getString("CNN");
Log.i("Feed", id);
}
}
catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater m_i_f = getMenuInflater();
m_i_f.inflate(R.menu.cool_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
//return super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.aboutus:
Toast.makeText(this, "Developed by Islam hamdy", Toast.LENGTH_LONG).show();
break;
case R.id.reference:
Toast.makeText(this, "Go To My blog", Toast.LENGTH_LONG).show();
break;
}
return false;
}
}
xml文件
> > <?xml version="1.0" encoding="utf-8"?> <LinearLayout
> > xmlns:android="http://schemas.android.com/apk/res/android"
> > android:layout_width="fill_parent"
> > android:layout_height="fill_parent"
> > android:orientation="vertical" >
> >
> >
> > <TextView
> > android:layout_width="wrap_content"
> > android:layout_height="fill_parent"
> > android:layout_gravity="center"
> > android:text="@string/hello"
> > android:id="@+id/Data_feed"
> > />
> >
> > </LinearLayout>
当我获取数据时,它就像一个网站 xml,并且语言没有出现
【问题讨论】:
-
具体是什么问题?另外请注意格式,使代码可读。
-
对不起,我现在让它可读
-
使用 xml 为移动设备传输数据需要多出 60% 的带宽,请考虑使用 JSON 作为替代方案来节省带宽。
-
你能编辑这段代码吗
标签: java android json eclipse exception