如何将解析后的值作为webview URL的字符串传递

时间:2014-04-03 20:41:33

标签: java android xml parsing webview

你好,我正在解析一个“密钥”,这是一个xml文件的url值,设置如此 单击我想要读取值作为字符串,作为要呈现的URL进入webview。那么如何将“Key”值中的解析值作为webview活动的url传递,以便将其作为字符串读取。提前感谢。

<article>
<id>Arts And Culture</id>
<title>Pete Seeger Died</title>
<author>Trent Erickson</author>
<duration>
Pete Seeger died yesterday. When I saw the news blurbs and Reddit posts, the name rang a bell,
</duration>
<thumb_url>
http://graffiti.hostoi.com/00Graffiti00/Photos/Art/pete.png
</thumb_url>
<key>
http://graffiti.hostoi.com/00Graffiti00/Articles/art/pete.html
</key>
</article>


public class Arti extends Activity {
// All static variables
static final String URL = "http://graffiti.hostoi.com/00Graffiti00/lists/arts.xml";
// XML node keys
static final String KEY_SONG = "song"; // parent node
static final String KEY_ID = "id";
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";
static final String KEY_DURATION = "duration";
static final String KEY_THUMB_URL = "thumb_url";
static final String KEY_LINK = "key";


ListView list;
LazyAdapteri adapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.arti);
    list=(ListView)findViewById(R.id.list);

    if(Utils.connectivity(getBaseContext()))
    {
        new ShowDialogAsyncTask().execute(URL);
        XMLParser parser = new XMLParser();
    }
    else
    {
        Toast.makeText(getBaseContext(), "Please connect to working internet connection.", Toast.LENGTH_SHORT).show();
    }

    new ShowDialogAsyncTask().execute();

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {

            Intent intent = new Intent(Arti.this, Article.class);
            intent.putExtra("key", KEY_LINK);

            startActivity(intent);



        }

    });

}

class ShowDialogAsyncTask extends AsyncTask<Void, Integer, Void> {
    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

    @Override
    protected void onPreExecute() {
        // update the UI immediately after the task is executed
        super.onPreExecute();
    }

    @Override
    protected Void doInBackground(Void... params) {

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML from URL
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_SONG);
        // looping through all song nodes <song>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value
            map.put(KEY_ID, parser.getValue(e, KEY_ID));
            map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
            map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
            map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
            map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

            // adding HashList to ArrayList
            songsList.add(map);
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(Integer... values) {
        super.onProgressUpdate(values);

    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);

        // Getting adapter by passing xml data ArrayList
        adapter = new LazyAdapteri(Arti.this, songsList);
        list.setAdapter(adapter);
    }

    public void execute(String url) {

    }
}
}

我的网页浏览设置如此

public class Article extends Activity{
private WebView webview;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.articleview);

    Bundle b = getIntent().getExtras();
    String KEY_LINK = b.getString("b");
    String url = getIntent().getStringExtra("key");

            webview = (WebView) findViewById(R.id.webView);



    webview.loadUrl(url);

    webview.loadUrl(KEY_LINK);



}


}

0 个答案:

没有答案