0

我想解析这个 xml api:http ://gdata.youtube.com/feeds/api/videos?max-results=50&q=honey 我正在把这个 api 的响应变成一个字符串,然后使用 sax 解析器解析它. 但是我在 parse 语句中得到空异常。

代码:

protected void onPostExecute(String result) 
{
super.onPostExecute(result);
try {
xr.parse(result);  //null exception
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

编辑:对不起,这是完整的代码:

public class MainActivity extends Activity implements OnClickListener {
static final String baseURL = "http://gdata.youtube.com/feeds/api/videos?max-results=50&q=";
static boolean flag=false;
EditText enter;
String tmp;
TextView label;
Button calc;
ListView lv;
listadapter la;
String arr[];
SAXParserFactory spf;
SAXParser sp;
XMLReader xr;
int index;
HandlingXMLStuff hxs;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.weatherxmlparse);
    enter = (EditText) findViewById(R.id.editcity);
    calc = (Button) findViewById(R.id.temp);
    lv = (ListView) findViewById(R.id.list);
    try {
        hxs=new HandlingXMLStuff();
        spf = SAXParserFactory.newInstance();
        sp = spf.newSAXParser();
        xr = sp.getXMLReader();
        xr.setContentHandler(hxs);
    } catch (Exception e) {
    }
    calc.setOnClickListener(this);
}

@Override
public void onClick(View arg0) {
    arr=new String[50];
    index=0;
    flag=false;
    Hashtable<String, String> hs = new Hashtable<String, String>();
    hs.put("", "");
    new getVideos().execute(hs);
    // la = new listadapter(this, arr);
    // lv.setAdapter(la);

    // } catch (Exception e) {
    // }
}

public class HandlingXMLStuff extends DefaultHandler {

    public String getInformation() {
        return "";
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        if (localName.equals("id")) {
            if(!flag)
                flag=true;
            else
            {
                String temp="";
String temp2=temp.replaceAll("http://gdata.youtube.com/feeds/api/videos/","");

            }
        }
    }

}

class getVideos extends
        AsyncTask<Hashtable<String, String>, String, String> {
    String resp = "";

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(Hashtable<String, String>... prmss) {
        StringBuilder URL = new StringBuilder(baseURL);
        String fullURL = URL.toString();
        try {
///String q = URLEncoder.encode(fullURL+enter.getText().toString(), "utf-8");
Hashtable<String,String> params=new Hashtable<String,String>();
            params.put("","");
            URL website = new URL(baseURL+enter.getText().toString());
String query = URLEncoder.encode(baseURL+enter.getText().toString(), "utf-8");
resp = ServerCom.getWeb(baseURL+enter.getText().toString(), params);
            tmp=resp;
            //bugged............
        } catch (Exception e) {
            e.printStackTrace();
            return resp;
        }
        return resp;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        try {
            xr.parse(result);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
}

忽略 ServerCom 类,因为它正在正确返回响应。

服务器通讯:

public class ServerCom
{
public static String getWeb(String Url , Hashtable<String, String> params) throws Exception
{
    String resp = callWebService(HttpGet.METHOD_NAME, Url, params);
    return resp;
}

public static String callWebService(String httpMethod, String URL,
        Hashtable<String, String> params) throws Exception {
    String sResponse = "";
    String resp = "";

    try 
    {
        Enumeration<String> e = params.elements();
        e = params.keys();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(URL);
        HttpGet getRequest = new HttpGet(URL);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        HttpResponse response;

        if (httpMethod.equalsIgnoreCase(HttpPost.METHOD_NAME)) {

            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = (String) params.get(key);
                reqEntity.addPart(key, new StringBody(value));
            }

            postRequest.setEntity(reqEntity);
            // postRequest.setEntity(reqEntityForFile);
            response = httpClient.execute(postRequest);
        } else
        {

            HttpParams httpParams = new BasicHttpParams();
            while (e.hasMoreElements()) {
                String key = (String) e.nextElement();
                String value = (String) params.get(key);
                // reqEntity.addPart(key, new StringBody(value));
                httpParams.setParameter(key, value);

            }
            getRequest.setParams(httpParams);
            response = httpClient.execute(getRequest);
        }

        BufferedReader reader = new BufferedReader(
                      new InputStreamReader(
    response.getEntity().getContent(), "UTF-8"));

        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
            resp = resp.concat(sResponse);
        }

    } catch (Exception e) {
        Log.e(e.getClass().getName(), e.getMessage());
    }
    Log.v("Im Coming app", "Response: " + resp);
    return resp;
}


}

编辑:

日志猫:http : //pastie.org/8383635

4

1 回答 1

0

看起来您尝试解析字符串,但 XMLReader 将解析 InputStream 或唯一资源,请查看文档XMLReader.parse(String systenid)

祝你好运!

于 2013-10-07T11:41:03.260 回答