地理位置无法在Android中运行

时间:2017-07-16 09:45:57

标签: android geolocation

我试图通过openweathermap获取天气信息,问题是它没有通过地理定位显示任何数据,我在真实设备上测试了我的应用程序,甚至确定清单中的权限

mainactivity

package com.example.vamshi.weathertest;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    static TextView place;
    static TextView temp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        place = (TextView)findViewById(R.id.place);
        temp = (TextView)findViewById(R.id.temp);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        String provider = locationManager.getBestProvider(new Criteria(),false);

        //Log.i("Jimmy",provider+" hi");
        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);
        Log.i("Jimmy","hi "+location);
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        Weather getData = new Weather();
        getData.execute("http://api.openweathermap.org/data/2.5/weather?lat=" + String.valueOf(lat) + "&lon=" + String.valueOf(lng) + "&appid=3c70f7d8f9e272cd6f73036a65228391");


    }
}

如果我从网址中删除" + String.valueOf(lat) + "" + String.valueOf(lng) + ",他工作正常。

天气活动

package com.example.vamshi.weathertest;

import android.os.AsyncTask;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Vamshi on 5/14/2017.
 */

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

    String result;
    @Override
    protected String doInBackground(String... urls) {
        result = "";
        URL link;
        HttpURLConnection myconnection = null;

        try {
            link = new URL(urls[0]);
            myconnection = (HttpURLConnection)link.openConnection();
            InputStream in = myconnection.getInputStream();
            InputStreamReader myStreamReader = new InputStreamReader(in);
            int data = myStreamReader.read();
            while(data!= -1){
                char current = (char)data;
                result+= current;
                data = myStreamReader.read();
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

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

        try {
            JSONObject myObject = new JSONObject(result);
            JSONObject main = new JSONObject(myObject.getString("main"));
            String temperature = main.getString("temp");
            String placeName = myObject.getString("name");

            MainActivity.place.setText(placeName);
            MainActivity.temp.setText(temperature);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

编辑

vertices.findAt

getData.execute("http://api.openweathermap.org/data/2.5/weather?lat=" + 
  String.valueOf(lat) + "&lon=" + String.valueOf(lng) + 
  "&appid=3c70f7d8f9e272cd6f73036a65228391");