计算距离时出错

问题描述:

我发现在计算距离时使用JSON的代码。我试图编辑它以在我的项目中使用它,但当单击按钮时出错。计算距离时出错

这里是我的代码:

public class MainActivity extends ActionBarActivity { 
EditText e1,e2; 
TextView t1; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    e1 = (EditText) findViewById (R.id.editText1); 
    e2 = (EditText) findViewById (R.id.editText2); 
    t1 = (TextView) findViewById (R.id.textView1); 
} 
public void d (View view){ 
    GetRoutDistane(0, 0, 0, 0); 
} 
public String GetRoutDistane(double startLat, double   startLong, double endLat, double endLong) 
{ 
    String Distance = "error"; 
    String Status = "error"; 
    try { 
     Log.e("Distance Link : ", "http://maps.googleapis.com/maps/api/directions/json?origin="+ startLat +e1+ startLong +"&destination="+ endLat +e2+ endLong +"&sensor=false"); 
     JSONObject jsonObj = parser_Json.getJSONfromURL("http://maps.googleapis.com/maps/api/directions/json?origin="+ startLat +e1+ startLong +"&destination="+ endLat +e2+ endLong +"&sensor=false"); 
     Status = jsonObj.getString("status"); 
     if(Status.equalsIgnoreCase("OK")) 
     { 
     JSONArray routes = jsonObj.getJSONArray("routes"); 
     JSONObject zero = routes.getJSONObject(0); 
     JSONArray legs = zero.getJSONArray("legs"); 
     JSONObject zero2 = legs.getJSONObject(0); 
     JSONObject dist = zero2.getJSONObject("distance"); 
     Distance = dist.getString("text"); 
     t1.setText(Distance.toString()); 
     } 
     else 
     { 
      Distance = "Too Far"; 
     } 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
return Distance; 


} 
public static class parser_Json { 

    public static JSONObject getJSONfromURL(String url){ 

     //initialize 
     InputStream is = null; 
     String result = ""; 
     JSONObject jArray = null; 

     //http post 
     try{ 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 

     }catch(Exception e){ 
      Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     try{ 
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      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()); 
     } 

     //try parse the string to a JSON object 
     try{ 
       jArray = new JSONObject(result); 
     }catch(JSONException e){ 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 

     return jArray; 
    } 

    public static InputStream retrieveStream(String url) { 

      DefaultHttpClient client = new DefaultHttpClient(); 

      HttpGet getRequest = new HttpGet(url); 

      try { 

       HttpResponse getResponse = client.execute(getRequest); 
       final int statusCode = getResponse.getStatusLine().getStatusCode(); 

       if (statusCode != HttpStatus.SC_OK) { 

        return null; 
       } 

       HttpEntity getResponseEntity = getResponse.getEntity(); 
       return getResponseEntity.getContent(); 

      } 
      catch (IOException e) { 
       getRequest.abort(); 

      } 

      return null; 

     } 
    } 

} 

的代码没有给出错误在Eclipse中,只是按一下按钮导致崩溃。

+1

也添加您的logcat。 – Exception

+0

重新启动您的eclipse,如果您的logcat不显示任何信息 –

+1

又一个NetworkOnMainThredException ...请在请求 – Selvin

您是否添加了从应用程序访问Internet的权限?如果不是因为这个原因可能会导致崩溃?此外,您正在主线程上执行网络操作,因此崩溃可能也是因为NetworkOnMainThreadException。在您检查了您的互联网许可后,请使用线程或Asynctask运行距离计算器代码。

+0

Internet权限是网络完全访问权限 –

+0

android.permission.INTERNET –

+0

即时通讯使用它mmmm else –