我在API 16上运行时遇到HTTP连接失败的问题,但在API 10上运行正常。其他网站连接使用相同的代码正常执行,因此特别是这个网站存在问题。创建连接并使用以下代码读入:(更新时间:02/04/14以包含整个测试程序)
package com.example.test2;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void getData(View view) {
new TaskGetDir().execute();
}
public class TaskGetDir extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
String gpx_url = "http://gpx.geotags.com/cgi-bin/gpx.cgi?saddr=Boston,MA%s&daddr=NewYork,NY%s";
String doc_gpx = "";
try {
HttpURLConnection conn = (HttpURLConnection) new URL(gpx_url).openConnection();
InputStream is = new BufferedInputStream(conn.getInputStream());
Reader reader = null;
reader = new InputStreamReader(is, "UTF-8");
char[] buffer = new char[512];
reader.read(buffer);
doc_gpx = new String(buffer);
} catch (IOException e) {
e.printStackTrace();
}
return doc_gpx;
}
protected void onPostExecute(String result) {
TextView out_loc1 = (TextView) findViewById(R.id.sample_out);
out_loc1.setText(result.toString());
}
}
}
代码正确地将512个字符提取到缓冲区中,并通过API10上的textView显示,但它与API 16崩溃。在conn.getContent()调用期间发生故障。 logcat日志记录如下所示:
01-31 07:42:34.096: E/AndroidRuntime(11036): FATAL EXCEPTION: AsyncTask #1
01-31 07:42:34.096: E/AndroidRuntime(11036): java.lang.RuntimeException: An error occured while executing doInBackground()
01-31 07:42:34.096: E/AndroidRuntime(11036): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-31 07:42:34.096: E/AndroidRuntime(11036): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.lang.Thread.run(Thread.java:856)
01-31 07:42:34.096: E/AndroidRuntime(11036): Caused by: java.lang.IndexOutOfBoundsException
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:156)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.lang.StringBuilder.append(StringBuilder.java:311)
01-31 07:42:34.096: E/AndroidRuntime(11036): at libcore.net.UriCodec.appendEncoded(UriCodec.java:114)
01-31 07:42:34.096: E/AndroidRuntime(11036): at libcore.net.UriCodec.appendPartiallyEncoded(UriCodec.java:142)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.net.URLStreamHandler.toExternalForm(URLStreamHandler.java:281)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.net.URL.toURILenient(URL.java:510)
01-31 07:42:34.096: E/AndroidRuntime(11036): at libcore.net.http.HttpEngine.<init>(HttpEngine.java:195)
01-31 07:42:34.096: E/AndroidRuntime(11036): at libcore.net.http.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:256)
01-31 07:42:34.096: E/AndroidRuntime(11036): at libcore.net.http.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:243)
01-31 07:42:34.096: E/AndroidRuntime(11036): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:78)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.net.URLConnection.getContent(URLConnection.java:190)
01-31 07:42:34.096: E/AndroidRuntime(11036): at com.example.test.MainActivity$TaskGetData.doInBackground(MainActivity.java:55)
01-31 07:42:34.096: E/AndroidRuntime(11036): at com.example.test.MainActivity$TaskGetData.doInBackground(MainActivity.java:1)
01-31 07:42:34.096: E/AndroidRuntime(11036): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-31 07:42:34.096: E/AndroidRuntime(11036): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
当该网站进入浏览器窗口时,会提示文件自动下载,然后从服务器端终止连接。它以xml格式返回我想要提取的lat / lon属性的方向。我的理论是,不同的API以不同的方式处理此尝试下载。任何帮助表示赞赏。
答案 0 :(得分:1)
我认为它与编码不正确的URL有关。
我用自己的HTTP客户端(DavidWebb)尝试了它:
public void testDownloadGpx() throws Exception {
Webb webb = Webb.create();
Response<String> response = webb
.get("http://gpx.geotags.com/cgi-bin/gpx.cgi")
.param("saddr", "Boston,MA%s")
.param("daddr", "NewYork,NY%s")
.asString();
assertEquals(200, response.getStatusCode());
assertNotNull(response.getBody());
assertTrue(response.getBody().contains("Turn right to stay on Tremont"));
}
// the same request in a shorter form
public void testDownloadGpxShort() throws Exception {
Webb webb = Webb.create();
String xml = webb
.get("http://gpx.geotags.com/cgi-bin/gpx.cgi")
.param("saddr", "Boston,MA%s")
.param("daddr", "NewYork,NY%s")
.asString()
.getBody();
assertTrue(xml.contains("Turn right to stay on Tremont"));
}
在调试器中,我可以看到URL编码如下:
http://.../gpx.cgi?saddr=Boston%2CMA%25s&daddr=NewYork%2CNY%25s
由于您的堆栈跟踪显示它在创建URL时崩溃,这可能是问题所在。 请使用上面的URL尝试一下。我希望它有效。