为什么我的json加载需要这么长时间?

时间:2016-02-26 20:27:00

标签: android json google-places-api

我正在构建一个应用程序,向用户显示最近的健身房,然后给他们指示。我正在使用Google Places Web API。该应用程序按其应用程序运行,但下载json需要24秒。这是为什么?

public class MainActivity extends AppCompatActivity implements LocationListener {
int REQUEST_PLACE_PICKER;
LocationManager locationManager;
String provider;
DownloadTask task;
Double lat;
Double lng;
ArrayList<String> gymName;
ArrayList<String> gymLat;
ArrayList<String> gymLng;


ListView gymNameList;
ArrayAdapter<String> gymNameAdapter;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    task = new DownloadTask();
    gymLat = new ArrayList<>();
    gymLng = new ArrayList<>();
    gymName = new ArrayList<>();
    gymNameList = (ListView) findViewById(R.id.listView);
    gymNameAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,gymName);
    gymNameList.setAdapter(gymNameAdapter);


gymNameList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Uri gmmIntentUri = Uri.parse("google.navigation:q=" + gymLat.get(position) + "," + gymLng.get(position));
    Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
    mapIntent.setPackage("com.google.android.apps.maps");
    startActivity(mapIntent);
}
});



    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    provider = locationManager.getBestProvider(new Criteria(), false);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = locationManager.getLastKnownLocation(provider);
    if (location != null){



        Toast.makeText(getApplicationContext(), "yay", Toast.LENGTH_SHORT).show();
        lat = location.getLatitude();
        lng = location.getLongitude();

        Log.e("Location info: Lat", lat.toString());
        Log.e("Location info: Lng", lng.toString());
    }
    else{

        Toast.makeText(getApplicationContext(), "damn", Toast.LENGTH_SHORT).show();
    }
    task.execute("https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=" + String.valueOf(lat) + "," + String.valueOf(lng) + "&radius=50000&type=gym&&key=AIzaSyCoOcFh0aduUuDMqw6TlUk5sjOpZjkKBAg");

}

@Override
public void onLocationChanged(Location location) {

    Double lat = location.getLatitude();
    Double lng = location.getLongitude();

    Log.i("Location info: Lat", lat.toString());
    Log.i("Location info: Lng", lng.toString());

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}


public class DownloadTask extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        String result = "";
        URL url;
        HttpURLConnection urlConnection;


        try {
            url = new URL(urls[0]);
            urlConnection = (HttpURLConnection) url.openConnection();
            InputStream in = urlConnection.getInputStream();
            InputStreamReader reader = new InputStreamReader(in);
            int data = reader.read();
            while (data != -1) {
                char current = (char) data;
                result += current;
                data = reader.read();
            }
            return result;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        try {
            JSONObject jsonObject = new JSONObject(result);
            String weatherContent = jsonObject.getString("results");

            Log.i("gymresults", weatherContent);
            JSONArray jsonArray = new JSONArray(weatherContent);



            for (int i = 0; i < jsonArray.length(); i++){


                JSONObject jsonPart = jsonArray.getJSONObject(i);
                JSONObject jsonLocation = jsonPart.getJSONObject("geometry").getJSONObject("location");
               String gymNName = jsonPart.getString("name");
                gymName.add(gymNName);
                gymNameAdapter.notifyDataSetChanged();
                String latLong = jsonPart.getString("geometry");
                String lat = jsonLocation.getString("lat");
                String lng = jsonLocation.getString("lng");

                Log.i("frick", lat);
                Log.i("frack", lng);

                gymLat.add(lat);
                gymLng.add(lng);

                Log.i("gymname", gymNName);
                Log.i("gymlocation", latLong);


            }



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

    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

0 个答案:

没有答案
相关问题