2014-09-24 43 views
0

我使用谷歌的距離矩陣API來獲取locations.So之間的距離我用這個查詢:httpclient.execute(httppost)拋出異常

String distanceofloc="https://maps.googleapis.com/maps/api/distancematrix/json?origins="+mylat+","+mylng+"&destinations="+placeloc+"&key=MY_BROWSER_KEY"; 

那時,我便用這個字符串爲獲得距離的函數:

String dis=setDistance(distanceofloc); 

這裏是我的setDistance功能:

public String setDistance(String url) throws URISyntaxException{ 
    String dis=null; 
    StringBuilder placesBuilder = new StringBuilder(); 

     //execute search 

     HttpClient placesClient = new DefaultHttpClient(); 
     HttpResponse placesResponse=null; 
      //try to fetch the data 
     try{ 
      HttpPost placesGet = new HttpPost(url); 


      try{ 
       placesResponse = placesClient.execute(placesGet); 

      }catch(Exception e){ 
       e.printStackTrace(); 
       Log.v("PLACES", "exception thrown"); 
      } 

      StatusLine placeSearchStatus = placesResponse.getStatusLine(); 



      if (placeSearchStatus.getStatusCode() == 200) { 
       //we have an OK response 


       HttpEntity placesEntity = placesResponse.getEntity(); 
       InputStream placesContent = placesEntity.getContent(); 
       //create reader 
       InputStreamReader placesInput = new InputStreamReader(placesContent); 
       //use buffered reader to process 
       BufferedReader placesReader = new BufferedReader(placesInput); 
       //read a line at a time, append to string builder 
       String lineIn; 
       while ((lineIn = placesReader.readLine()) != null) { 
        placesBuilder.append(lineIn); 
       }}} 
       catch(Exception e){ 
        e.printStackTrace(); 
        Log.v("PLACES", "missing value"); 
       } 




    try { 
     //parse JSON 

     //create JSONObject, pass stinrg returned from doInBackground 

     JSONObject distanceJSONObject = new JSONObject(placesBuilder.toString()); 

     JSONArray rowArray = distanceJSONObject.getJSONArray("rows"); 

     JSONObject rowsObject = rowArray.getJSONObject(0);//only one element in this array 


     JSONArray elementsArray = rowsObject.getJSONArray("elements"); 

     JSONObject elementsObject = elementsArray.getJSONObject(0);//only one element in this array 
     if(elementsObject.isNull("distance")){ 

     } 
     else{ 
     JSONObject distanceObject = elementsObject.getJSONObject("distance"); 
     if(!distanceObject.isNull("text")) 
     { 
     dis=distanceObject.getString("text" 



     } 

    } 



    }catch (JSONException e) { 
     e.printStackTrace(); 

    } 


    return dis; 

} 

這裏,異常是在

HttpPost placesGet = new HttpPost(url); 


      try{ 
       placesResponse = placesClient.execute(placesGet); 

      }catch(Exception e){ 
       e.printStackTrace(); 
       Log.v("PLACES", "exception thrown"); 
      } 

拋出誰能告訴我,如果事情是錯的網址是什麼?任何幫助表示讚賞。

+2

請發佈您的logcat – DroidDev 2014-09-24 13:03:28

回答

0

您不允許在主UI線程上執行任何網絡io。 Android Framework提供瞭解決此限制的特定方法。您應該使用Worker Threads or AsyncTask來處理您所做的所有網絡相關事宜。

編輯:

這是我的原始建議。這可能對其他人有用。

從我的頭腦開始,您是否有權在您的Manifest.xml上訪問互聯網?一些東西沿線

<manifest ...> 
    ... 
    <uses-permission android:name="android.permission.INTERNET"></uses-permission> 
</manifest> 

這是衆所周知的嘗試連接到任何服務器時會導致異常。

+0

是的,我已經在我的Manifest.xml中添加了這個。 – Pushpendu 2014-09-24 13:18:42

+0

你能發佈你得到的異常嗎? – Chnoch 2014-09-24 13:19:21

+0

android.os.NetworkOnMainThreadException – Pushpendu 2014-09-24 13:30:02