0

我在这段代码中得到了一个 NPE,我需要很快修复,有人可以帮帮我吗?

import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.os.AsyncTask;

public class GetXML extends AsyncTask<String, String, BirdData> {

public GetXML(){

}

/**
 * background
 */
@Override
protected BirdData doInBackground(String... params) {
    String GetWikiID = params[0];
 // All static variables
    String URL = "http://www.bbc.co.uk/nature/life/"+GetWikiID+".rdf";
    // XML node keys

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL); // getting XML
    Document doc = parser.getDomElement(xml); // getting DOM element
    System.out.println("test3");
    System.out.println(doc.getElementById("dc:description").getTextContent());
    return null;
}

/**
 * on getting result
 */
@Override
protected void onPostExecute(BirdData result) {

}
}

我正在使用此代码来调用它:

System.out.println(WikiID);
new GetXML().execute(WikiID);

错误是这样的:

    08-08 16:04:17.284: E/AndroidRuntime(17971): FATAL EXCEPTION: AsyncTask #1
08-08 16:04:17.284: E/AndroidRuntime(17971): java.lang.RuntimeException: An error occured while executing doInBackground()
08-08 16:04:17.284: E/AndroidRuntime(17971):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.lang.Thread.run(Thread.java:856)
08-08 16:04:17.284: E/AndroidRuntime(17971): Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 45: http://www.bbc.co.uk/nature/tools/search/Type text here...
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.net.URI.create(URI.java:727)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at org.apache.http.client.methods.HttpPost.<init>(HttpPost.java:79)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at com.swift.birdspot.JSONParser.getJSONFromUrl(JSONParser.java:36)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at com.swift.birdspot.GetWikiID.doInBackground(GetWikiID.java:31)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at com.swift.birdspot.GetWikiID.doInBackground(GetWikiID.java:1)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-08 16:04:17.284: E/AndroidRuntime(17971):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-08 16:04:17.284: E/AndroidRuntime(17971):    ... 5 more
4

2 回答 2

1

错误是以下 URL 上有无效字符(空格字符):

http://www.bbc.co.uk/nature/tools/search/Type text here...

您应该修复此 URL,您的问题将得到解决!

于 2013-08-08T15:46:34.317 回答
0

原来我需要做的就是在我的 jsonparser 类中进行 url 编码。

于 2013-08-10T01:22:32.657 回答