0

この xml api を解析したい: http://gdata.youtube.com/feeds/api/videos?max-results=50&q=honey この api からの応答を文字列に取得し、sax パーサーを使用して解析します. しかし、解析ステートメントで null 例外が発生しています。

コード:

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


}

編集:

logcat: http://pastie.org/8383635

4

1 に答える 1