0

我正在尝试解析包含描述标签的 XML 以从中提取内容(摘要)。活动意外关闭/停止工作。您可以访问 URL 中的链接并添加您的查询以查看我尝试解析的 XML 结果。

public class ResultActivity extends Activity{ //implements TextToSpeech.OnInitListener {
 InputStream is = null;
 private TextView textView;
 XmlPullParserFactory pullParserFactory;
 private static final String DEBUG_TAG = "HttpExample";
 String query,result;
 //private TextToSpeech tts;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,        //Code for full screen activity
    WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_result);
    Intent i = getIntent();
    query = i.getStringExtra("query");
    textView = (TextView) findViewById(R.id.mytext);
    myClickHandler();
    //speakout();

}

public void myClickHandler() {
        query.replace(' ', '_');
        String stringUrl = "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryClass=place&QueryString="+query;
        ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            new DownloadWebpageTask().execute(stringUrl);
        } else {
            textView.setText("No network connection available.");
        }
        }



/*NEW subCLASS*/
class DownloadWebpageTask extends AsyncTask<String, Void, String> {

    String ns=null;
    @Override
protected String doInBackground(String... urls) {

        // params comes from the execute() call: params[0] is the url.
        try {
            return downloadUrl(urls[0]);
        } catch (IOException e) {
            return "Unable to retrieve web page. URL may be invalid.";
        }
    }

private String downloadUrl(String myurl) throws IOException {
          try {
                URL url = new URL(myurl);
                HttpURLConnection.setFollowRedirects(true);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(10000 /* milliseconds */);
                conn.setConnectTimeout(15000 /* milliseconds */);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                // Starts the query
                conn.connect();
                int response = conn.getResponseCode();
                Log.d(DEBUG_TAG, "The response is: " + response);
                is = conn.getInputStream();

               pullParserFactory = XmlPullParserFactory.newInstance();
                XmlPullParser parser = pullParserFactory.newPullParser();
                parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                parser.setInput(is, null);
               // result=convertStreamToString(is);
                result=parseXML(parser);
              }
               catch (XmlPullParserException e) {
                    e.printStackTrace();
               }    finally {
                if (is != null) {
                    is.close();
                } 
            }
            return result;
        }
    // onPostExecute displays the results of the AsyncTask.


     @Override
protected void onPostExecute(String result) {
        textView.setText(result);
     }
//FINAL TEXT VIEW WIDGET DISPLAY


private String parseXML(XmlPullParser parser) throws XmlPullParserException, IOException {
        String res = null;
        int eventType = parser.getEventType();

         while (eventType != XmlPullParser.END_DOCUMENT) {
             String name = parser.getName();
             if(eventType == XmlPullParser.START_DOCUMENT) {
              System.out.println("Start document");
              textView.setText("Reached");
          } else if(eventType == XmlPullParser.END_DOCUMENT) {
              System.out.println("End document");
          }
          else if(name.equals("description")) {
              res=readSummary(parser);
          } 
          eventType = parser.next();
         }
    return res;
    }

    //PARSE OUT THE DESCRIPTION TAG     

private String readSummary(XmlPullParser parser) throws IOException, XmlPullParserException {
        parser.require(XmlPullParser.START_TAG, ns, "description");
        String summary = readText(parser);
        parser.require(XmlPullParser.END_TAG, ns, "description");
        return summary;
    }     

    //PARSE OUT THE DESCRIPTION CONTENT FROM THE TAG    

private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
                String result = "";
                if (parser.next() == XmlPullParser.TEXT) {
                    result = parser.getText();
                    parser.nextTag();
                }
                return result;

    }       

}


}

我的日志猫

06-19 19:47:08.344: E/Trace(662): error opening trace file: No such file or directory (2)
06-19 19:47:09.666: D/gralloc_goldfish(662): Emulator without GPU emulation detected.
06-19 19:49:32.676: D/HttpExample(662): The response is: 200
06-19 19:49:36.036: D/dalvikvm(662): GC_CONCURRENT freed 248K, 3% free 10985K/11271K, paused 79ms+40ms, total 289ms
06-19 19:53:21.125: D/gralloc_goldfish(716): Emulator without GPU emulation detected.
06-19 19:53:53.435: D/HttpExample(716): The response is: 200
06-19 19:53:53.465: I/System.out(716): Start document
06-19 19:53:53.475: W/dalvikvm(716): threadid=11: thread exiting with uncaught exception (group=0x40a13300)
06-19 19:53:53.535: E/AndroidRuntime(716): FATAL EXCEPTION: AsyncTask #1
06-19 19:53:53.535: E/AndroidRuntime(716): java.lang.RuntimeException: An error occured while executing doInBackground()
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.os.AsyncTask$3.done(AsyncTask.java:299)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.lang.Thread.run(Thread.java:856)
06-19 19:53:53.535: E/AndroidRuntime(716): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4609)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.View.requestLayout(View.java:15129)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.View.requestLayout(View.java:15129)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.View.requestLayout(View.java:15129)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.View.requestLayout(View.java:15129)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.view.View.requestLayout(View.java:15129)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.widget.TextView.checkForRelayout(TextView.java:6309)
06-19 19:53:53.535: E/AndroidRuntime(716):  at     android.widget.TextView.setText(TextView.java:3547)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.widget.TextView.setText(TextView.java:3405)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.widget.TextView.setText(TextView.java:3380)
06-19 19:53:53.535: E/AndroidRuntime(716):  at com.example.verch.ResultActivity$DownloadWebpageTask.parseXML(ResultActivity.java:150)
06-19 19:53:53.535: E/AndroidRuntime(716):  at com.example.verch.ResultActivity$DownloadWebpageTask.downloadUrl(ResultActivity.java:122)
06-19 19:53:53.535: E/AndroidRuntime(716):  at com.example.verch.ResultActivity$DownloadWebpageTask.doInBackground(ResultActivity.java:96)
06-19 19:53:53.535: E/AndroidRuntime(716):  at com.example.verch.ResultActivity$DownloadWebpageTask.doInBackground(ResultActivity.java:1)
06-19 19:53:53.535: E/AndroidRuntime(716):  at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-19 19:53:53.535: E/AndroidRuntime(716):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-19 19:53:53.535: E/AndroidRuntime(716):  ... 5 more
06-19 19:53:57.464: I/Process(716): Sending signal. PID: 716 SIG: 9
4

2 回答 2

0

我不认为这是一个解析错误。正如异常所说,除非它是 UI 线程,否则您无法处理线程内的视图。在后台线程上运行的 parseXML 中,您尝试在视图上设置文本。

于 2013-06-20T17:51:59.067 回答
0

尝试这个 :

parser.require(XmlPullParser.START_TAG, ns, "description");
String summary = parser.nextText();
parser.require(XmlPullParser.END_TAG, ns, "description");
于 2013-06-20T15:20:36.217 回答