Android中的RSS Feed异常

时间:2013-12-15 08:30:34

标签: java android json eclipse exception

我想在Android上的设备中查看我的文本框中的新闻我已经获取了xml数据,但我无法解析我的数据以获取我的新闻

RSSFeedActivity Class

  

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;
    }
}

XMLFILE

>     > <?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,而且语言没有出现

1 个答案:

答案 0 :(得分:0)

您正在尝试使用解析JSON的代码解析XML响应,这是一个错误。我建议使用托管服务器将xml响应转换为JSON。这样做的PHP代码就是。

<?php
$url="your_url_here";
$fileContents= file_get_contents($url);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
echo $json;
?>

在某个地方托管这个并调用此页面来转换您的Feed,这将导致移动客户端的带宽消耗降低。完成后,就像在代码中一样解析响应!

此外,您的代码尝试在其主线程上执行网络操作。这是非常沮丧的!尝试以上android 2.3您的应用程序会立即崩溃!请参考使用AsyncTask类来避免在主线程上进行长时间运行!