2014-09-24 45 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