计算距离时出错

时间:2015-04-23 03:29:45

标签: android json dictionary distance

我发现这个代码在计算距离时使用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 个答案:

答案 0 :(得分:0)

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