在 android 中调用 httpget 时出错(检索 google 方向)
Posted
技术标签:
【中文标题】在 android 中调用 httpget 时出错(检索 google 方向)【英文标题】:error calling httpget in android (retrieve google directions) 【发布时间】:2012-05-29 10:28:05 【问题描述】:我正在尝试使用 android httpget 调用此 URL 以获得一个 json 方向数组:http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true%7cBarossa+Valley,SA%7cClare,SA%7cConnawarra,SA%7cMcLaren+Vale,SA&sensor=false"
基本上这是我到目前为止所做的:
1) 我首先创建了一个异步任务,以便调用 google 路线并记录检索到的结果:
public class MyTask extends AsyncTask<String, Integer, String>
@Override
protected String doInBackground(String... params)
InputStream is = null;
String result = null;
try
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(params[0]);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
int lenght = (int) entity.getContentLength();
StringBuffer buffer = new StringBuffer(lenght);
is = entity.getContent();
catch(Exception e)
Log.e("log_tag", "Error in http connection"+e.toString());
try
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null)
sb.append(line + "\n");
is.close();
result = sb.toString();
catch(Exception e)
Log.e("log_tag", "Error converting result "+e.toString());
Log.i("Result", result);
return result;
2) 在主 Activity 中,我将执行传递给它的 url 的异步任务:
MyTask t = new MyTask();
t.execute(urlString.toString());
其中 urlString 是一个 StringBuilder。我尝试以多种方式构建该地址,即使尝试使用 URLEncoder.encode(myUrl) 对其进行编码,但我总是得到一个异常,即 Error in http connectionjava.lang.NegativeArraySizeException 我无法从谷歌检索 json 数据。如何正确格式化该网址?我的目标是达到和这个人一样的结果(在 json 部分):http://blog.synyx.de/2010/06/routing-driving-directions-on-android-part-1-get-the-route/
谢谢!
【问题讨论】:
logcat 中的 Stacktrace 应该指向失败的行,发布它;p 嗯,它指向以下行 StringBuffer buffer = new StringBuffer(lenght);所以看起来httpget的响应是空的(可能是因为请求格式错误!)。 但是还是不行! 【参考方案1】:我终于明白了!我改变了调用谷歌网络服务的方式。基本上这部分:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(params[0]);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
用这种其他方式调用谷歌网络服务:
HttpURLConnection urlConnection = null;
url = new URL("http://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true%7cBarossa+Valley,SA%7cClare,SA%7cConnawarra,SA%7cMcLaren+Vale,SA&sensor=false");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
is = urlConnection.getInputStream();
urlConnection.connect();
然后我“链接”了 InputStreamReader 中的 Input Stream,Buffered Reader 中的 InputStreamReader,一切正常:
InputStreamReader inputStream = new InputStreamReader(is);
BufferedReader r = new BufferedReader(inputStream);
String line = null;
line = r.readLine();
while (line!=null)
Log.i("RESULT", line);
line = r.readLine();
通过这种方式,我能够记录所需的结果。我想了解的一件事是为什么 httpclient 不起作用,但 httpURLConnection 却起作用。谁能帮我?
【讨论】:
【参考方案2】:只要删除有问题的代码,因为它没有在任何地方使用?
//int lenght = (int) entity.getContentLength();
//StringBuffer buffer = new StringBuffer(lenght);
【讨论】:
以上是关于在 android 中调用 httpget 时出错(检索 google 方向)的主要内容,如果未能解决你的问题,请参考以下文章
React-native Android:调用 AppRegistry.runApplication 时出错
在空对象引用上调用虚拟方法“double android.location.Location.getLatitude()”时出错
使用 BroadcastReceiver 实现 Android 通知操作时出错