gpt4 book ai didi

android - 使用 PullParser 解析属性值的方法

转载 作者:行者123 更新时间:2023-11-30 04:35:00 24 4
gpt4 key购买 nike

我想解析谷歌天气 Api 响应.. 响应如下:

  <current_conditions>
<condition data="Haze"/>
<temp_f data="82"/>
<temp_c data="28"/>
<humidity data="Humidity: 94%"/>
<icon data="/ig/images/weather/haze.gif"/>
<wind_condition data="Wind: SW at 12 mph"/>
</current_conditions>

now i want only temp_c data to use in my app.
so how can i get the value of temp using xml pull parser..

right now i m doing like that

StringBufferInputStream buffer = new StringBufferInputStream(response);
xpp = XmlPullParserFactory.newInstance().newPullParser();
xpp.setInput(buffer, null);
int eventType = xpp.getEventType();
while(eventType != XmlPullParser.END_DOCUMENT)
{


if(eventType == XmlPullParser.START_TAG){

String elementName = xpp.getName();
if(elementName.equals("current_conditions")){
local_pickup = xpp.nextText();
Log.d("database", local_pickup);
}
}
eventType = xpp.next();
}
buffer.close();

请大家尽早帮助我

最佳答案

这是使用 Android XML Pull Parser 解析单个值的一个很好的例子。

除了上面的链接

package com.news.search;

import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;

import org.xmlpull.v1.XmlPullParser; import android.util.Xml;

import com.yahoo.search.ImageThumbnail; import com.yahoo.search.NewsSearchResult; import com.yahoo.search.xmlparser.XmlParserImageThumbnail; import com.yahoo.xml.XmlParser;


public class NewsPullParser {

private InputStream input;
public final static String TITLE = "Title";
public final static String SUMMARY = "Summary";
public final static String URL = "Url";
public final static String CLICK_URL = "ClickUrl";
public final static String SOURCE = "NewsSource";
public final static String SOURCE_URL = "NewsSourceUrl";
public final static String LANGUAGE = "Language";
public final static String PUBLISH_DATE = "PublishDate";
public final static String MOD_DATE = "ModificationDate";
public static ArrayList newsTags;
static{ newsTags = new ArrayList();
newsTags.add(TITLE);
newsTags.add(SUMMARY);
newsTags.add(URL);
newsTags.add(CLICK_URL);
newsTags.add(SOURCE);
newsTags.add(SOURCE_URL);
newsTags.add(LANGUAGE);
newsTags.add(PUBLISH_DATE);
newsTags.add(MOD_DATE);
}



public NewsPullParser(InputStream stream){ this.input = stream; }
public NewsSearchResult[] parse(List TagFilter) throws Exception {
ArrayList content = null;
XmlPullParser parser = Xml.newPullParser();
try{

parser.setInput(this.input,"UTF-8");
HashMap map = null;
int event = parser.getEventType();

while (event != XmlPullParser.END_DOCUMENT){
String name = null;

switch (event){

case XmlPullParser.START_DOCUMENT: content = new ArrayList();
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("Result")){
map = new HashMap();
}else if(map != null && newsTags.contains(name) && TagFilter.contains(name)){

map.put(name,parser.nextText());

}
break;

case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("RESULT") && map != null)
content.add(new PullParserNewsSearchResult(map));

break;
}

event = parser.next();
}

}catch(Exception exp){ throw exp; }
return content.toArray(new NewsSearchResult[content.size()]); }

private class PullParserNewsSearchResult implements NewsSearchResult {
private Map result;


public PullParserNewsSearchResult(Map result) {
this.result = result;
}

public String getTitle() {
return (String)result.get(TITLE);
}

public String getSummary() {
return (String)result.get(SUMMARY);
}

public String getUrl() {
return (String)result.get(URL);
}

public String getClickUrl() {
return (String)result.get(CLICK_URL);
}

public String getNewsSource() {
return (String)result.get(SOURCE);
}

public String getNewsSourceUrl() {
return (String)result.get(SOURCE_URL);
}

public String getLanguage() {
return (String)result.get(LANGUAGE);
}

public String getPublishDate() {
return (String)result.get(PUBLISH_DATE);
}

public String getModificationDate() {
return (String)result.get(MOD_DATE);
}

public ImageThumbnail getThumbnail() {
return null;
}
}
}

现在业务结束

public void run(){

request.getParameters().put("appid", "javasdktest");
// request.setResults(15);


do {
while(!this.anyRequest){

try{
synchronized (this) {
this.wait();
}
}catch(InterruptedException exp){
////Just Nothing
}

}

try{

NewsPullParser parser = new NewsPullParser(RestClient.call(request.getRequestUrl(), request.getParameters()));
ArrayList filter = new ArrayList();
filter.add(NewsPullParser.TITLE);
filter.add(NewsPullParser.SUMMARY);

Bundle content = new Bundle();
content.putSerializable("result",parser.parse(filter));
Message msg = new Message();
msg.setData(content);
this.callback.sendMessage(msg);


}catch(Exception exp){
exp.printStackTrace();

}


this.anyRequest = false;
this.lastRequest_finished = true;

}while(!this.stopFlag);

}

关于android - 使用 PullParser 解析属性值的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7007023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com