HTTP请求失败

时间:2011-01-14 17:00:10

标签: android

我必须得到这样的HTTP请求:

http://www.aroundertouch.com/queriesandroid1dot0/project/idProjet/45.484171;;9.050393;;Rotondi Group S.R.L. - Costruzione Macchine Abbigliamento/iphone/354781043339495/lang/en_US

使用此代码:

url = new URL(query);
   Log.d(TAG,url.toString());
      connection = url.openConnection();
      httpConnection = (HttpURLConnection)connection; 
      int responseCode = httpConnection.getResponseCode(); 
      // HTTP status okay
      if (responseCode == HttpURLConnection.HTTP_OK) { 
       // preparo stream 
       in = httpConnection.getInputStream();
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder();
       dom = db.parse(in);      
      }
      // HTTP status fail 
      else{
       throw new Exception("responseCode != 200 OK in "+query);
      }
      in.close();
      httpConnection.disconnect();
      _ret = parseXML(dom);

我遇到这个例外,我怀疑它是由未编码的网址引起的,我该如何编码我的网址?它不是由HTTPURLConnection自动编码的? 错误时抛出:

D/dalvikvm(10100): GC freed 3447 objects / 513168 bytes in 47ms
W/System.err(10100): org.xml.sax.SAXParseException: Unexpected end of document
W/System.err(10100):    at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:129)
W/System.err(10100):    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:157)
W/System.err(10100):    at granet.arounder.webservice.ThreadDatiCard.doInBackground(ThreadDatiCard.java:76)
W/System.err(10100):    at granet.arounder.webservice.ThreadDatiCard.doInBackground(ThreadDatiCard.java:1)
W/System.err(10100):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
W/System.err(10100):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
W/System.err(10100):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
W/System.err(10100):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
W/System.err(10100):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
W/System.err(10100):    at java.lang.Thread.run(Thread.java:1096)

非常感谢。

1 个答案:

答案 0 :(得分:2)

是的,您网址中的数据包含的空格很糟糕,可能导致您的请求无法返回您希望解析的内容,请使用内置的urlencoder。

http://developer.android.com/reference/java/net/URLEncoder.html