2012-05-29 4 views
0

를 (구글 방향을 검색) httpget 전화 : 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 "오류가 안드로이드에 나는 방향의 JSON 배열하도록 httpget 안드로이드이 URL을 호출하기 위해 노력하고있어

하는 것은 기본적으로 이것은 내가 지금까지 한 일이다 : 여기에 URL을 통과하는 비동기 작업을 실행

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) 주요 활동에서, 내가 만들어 줄게 :

1) 내가 먼저 구글 방향을 호출하고 검색 결과를 기록하기 위해에 AsyncTask를 생성 :

MyTask t = new MyTask(); 
    t.execute(urlString.toString()); 

여기서 urlString은 StringBuilder입니다. URLEncoder.encode (myUrl)로 인코딩하려고해도 여러 가지 방법으로 그 주소를 만들려고했지만 항상 예외가 발생합니다. http connectionjava.lang.NegativeArraySizeException의 오류에 오류가 있습니다. Google에서 json 데이터를 검색하지 않습니다. 어떻게하면 해당 URL을 올바르게 포맷 할 수 있습니까? 내 목표는이 남자가했던 것과 같은 결과를 달성하는 것입니다 (json 부분에서) : http://blog.synyx.de/2010/06/routing-driving-directions-on-android-part-1-get-the-route/

고마워요!

+0

logcat의 스택 추적은 실패한 행을 가리켜 야합니다. 게시하십시오; –

+0

글쎄 다음 행을 가리 킵니다. StringBuffer buffer = new StringBuffer (lenght); 그래서 그것은 HTTPget에서 응답이 비어있는 것 같습니다 (아마도 형식화되지 않은 요청 때문에!). – jiraya85

+0

그러나 아직 작동하지 않습니다! – jiraya85

답변

1

마침내 얻습니다! 나는 Google webservice가 호출되는 방식을 변경했습니다. 기본적으로이 부분 :하는 InputStreamReader, 버퍼 Reader 및 모든 작품 내부에 InputStreamReader 내부

 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(); 

다음 나는 "체인"입력 스트림 : 구글 웹 서비스를 호출이 다른 방법으로

 HttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet(params[0]); 
    HttpResponse response = httpclient.execute(httpget); 
    HttpEntity entity = response.getEntity(); 

괜찮음 :

 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이 대신하는 이유입니다. 누구든지 나를 도울 수 있습니까?

0

아무데도 사용되지 않으므로 문제가되는 코드를 제거하면됩니까?

//int lenght = (int) entity.getContentLength(); 

//StringBuffer buffer = new StringBuffer(lenght);