在android中调用httpget时出错(检索google方向)
我试图用android httpget调用此URL,以获得一个json方向数组: 基本上这就是我到目前为止所做的: 1)为了调用google directions并记录检索到的结果,我首先创建了一个asynctask:在android中调用httpget时出错(检索google方向),android,web-services,http-get,directions,Android,Web Services,Http Get,Directions,我试图用android httpget调用此URL,以获得一个json方向数组: 基本上这就是我到目前为止所做的: 1)为了调用google directions并记录检索到的结果,我首先创建了一个asynctask: public class MyTask extends AsyncTask<String, Integer, String> { @Override protected String doInBackground(String... params) { Input
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;
}
}
其中urlString是一个StringBuilder。我尝试了几种方法来构建该地址,甚至尝试使用URLEncoder.encode(myUrl)对其进行编码但是我总是会遇到一个异常,那就是http connectionjava.lang.NegativeArraySizeException中的错误,我无法从google检索json数据。我如何正确格式化该url?我的目标是实现与这个家伙(在json部分)相同的结果:
谢谢!请删除有问题的代码,因为它没有在任何地方使用
//int lenght = (int) entity.getContentLength();
//StringBuffer buffer = new StringBuffer(lenght);
我终于明白了!我改变了google Web服务的调用方式。基本上这部分:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(params[0]);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
用另一种方式调用google Web服务:
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中,将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却工作。有人能帮我吗?logcat中的Stacktrace应该指向失败的行,post;pWell它指向下一行StringBuffer=newStringBuffer(lenght);因此来自httpget的响应似乎是空的(可能是因为请求格式错误!)。但是它仍然不能工作!
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();
}