我的应用程序未能以任何可能的解释登录

时间:2018-04-14 22:25:52

标签: java android json firebase logcat

该应用程序只是不会移动到登录页面,错误指向我内置的文档

04-14 22:10:48.482 4206-4206 / com.onyebuchboss.bossweatherbitcoinapp E / StorageHelpers:版本无价值                                                                                           org.json.JSONException:版本没有值                                                                                               at org.json.JSONObject.get(JSONObject.java:389)                                                                                               在org.json.JSONObject.getString(JSONObject.java:550)                                                                                               在com.google.firebase.auth.internal.zzz.zzc(未知来源)                                                                                               在com.google.firebase.auth.internal.zzz.zzbi(未知来源)                                                                                               在com.google.firebase.auth.FirebaseAuth。(未知来源)                                                                                               在com.google.firebase.auth.FirebaseAuth。(未知来源)                                                                                               在com.google.firebase.auth.internal.zzj。(未知来源)                                                                                               在com.google.firebase.auth.FirebaseAuth.zza(未知来源)                                                                                               在com.google.firebase.auth.FirebaseAuth.getInstance(未知来源)                                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                                               在com.google.firebase.FirebaseApp.zza(未知来源)                                                                                               在com.google.firebase.FirebaseApp.zzc(未知来源)                                                                                               在com.google.firebase.FirebaseApp.initializeApp(未知来源)                                                                                               在com.google.firebase.FirebaseApp.initializeApp(未知来源)                                                                                               在com.google.firebase.FirebaseApp.initializeApp(未知来源)                                                                                               在com.google.firebase.provider.FirebaseInitProvider.onCreate(未知来源)                                                                                               在android.content.ContentProvider.attachInfo(ContentProvider.java:1748)                                                                                               在android.content.ContentProvider.attachInfo(ContentProvider.java:1723)                                                                                               在com.google.firebase.provider.FirebaseInitProvider.attachInfo(未知来源)                                                                                               在android.app.ActivityThread.installProvider(ActivityThread.java:5153)                                                                                               在android.app.ActivityThread.installContentProviders(ActivityThread.java:4748)                                                                                               在android.app.ActivityThread.handleBindApplication(ActivityThread.java:4688)                                                                                               在android.app.ActivityThread.-wrap1(ActivityThread.java)                                                                                               在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1405)                                                                                               在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                               在android.os.Looper.loop(Looper.java:148)                                                                                               在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                               at java.lang.reflect.Method.invoke(Native Method)                                                                                               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

我的代码

public class WeatherActivity extends AppCompatActivity {

    final int REQUEST_CODE = 123;
    //the openweather url to be used
    final String WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather";
    //the API key
    final String APP_ID = "c9e6cb73daaf09d1bf0d1502d964bf60";
    //time between location update (5000 milliseconds or 5 seconds)
    final long MIN_TIME = 5000;
    //Distance between location in metres
    final float MIN_DISTANCE = 1000;
    final String TAG = "WeatherApp";

    //For the app to detect the user's location,
    //we set the LOCATION_PROVIDER
    String LOCATION_PROVIDER = LocationManager.GPS_PROVIDER;


    //declare the variables to be linked to the layout
    TextView mTemperature;
    TextView mCity;
    ImageView mWeatherImage;

    //declare the LocationListener and LocationManager
    LocationManager mLocationManager;
    LocationListener mLocationListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.weather_layout);

        //Link the elements in the Layout here
        mCity = (TextView) findViewById(R.id.locationFetching);
        mTemperature = (TextView) findViewById(R.id.tempereatureView);
        mWeatherImage = (ImageView) findViewById(R.id.weatherImage);
        ImageButton ChangeCityButton = (ImageButton) findViewById(R.id.changeCityButton);


        //The Intent method used below is mostly used for switching between Activities
        ChangeCityButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent changeCityIntent = new Intent(WeatherActivity.this, ChangeCityController.class);
               // finish();
                startActivity(changeCityIntent);
            }
        });
    }


    //the onResume method is one of android lifecycle method
    //that starts/execute after the onCreate method
    @Override
    protected void onResume() {
        super.onResume();

        //retrieving the city name passed in the
        //ChangecityController
        //City was used in the PutExtra method in changeCity
        Intent myIntent = getIntent();
        String city =myIntent.getStringExtra("City");

        //if the user does not enter a particular city
        //retrieve user current city
        if(city != null) {
            getWeatherForNewLocation(city);
        } else {
            getWeatherForCurrentLocation();
        }
    }

    //The method to retrieve any city entered by the user
    private void getWeatherForNewLocation(String city) {
        //the request params passes in the required parameters to retrieve data using the API
        //The openWeatherMap API being used in this project, "q" and acity's name
        //is to be assigned to iy
        RequestParams params = new RequestParams();
        params.put("q", city);
        params.put("appid", APP_ID);
        networkingCalls(params);
    }

    //  get weather situation for current city -getWeatherForCurrentCityHere()
    private void getWeatherForCurrentLocation() {
        //create an instance of LocationManager
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        //the Location listener does the checking of the location for update
        mLocationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d(TAG, "onLocationChanged: callback received");

                //get the longitude and latitude of current locaion
                //stored as a string
               String Longitude =  String.valueOf(location.getLongitude());
              String Latitude =  String.valueOf(location.getLatitude());

                Log.d(TAG, "onLocation Latitude is: " + Latitude);
                Log.d(TAG, "onLocationChanged: longitude " + Longitude);

                RequestParams params =  new RequestParams();

                params.put("lat", Latitude);
                params.put("lon", Longitude);
                params.put("appid", APP_ID);

                networkingCalls(params);

            }

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

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Log.d(TAG, "onProviderDisabled: callback");

            }
        };

        //REQUEST A LOCATION UPDATE PASSING THE LOCATION PROVIDER TO BE USED, MIN TIME,
        // MIN DISTANCE AND mLISTENER as the receiver
        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.
            //request permissions to use the user's device GPS
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
            return;
        }
        mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
    }


    //the override method below gives the result of the
    // permission request to use the user's GPS
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        //check to see if the request code matches the Request Code we gave
        //during the request
        if(requestCode == REQUEST_CODE){
            if(grantResults.length> 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Log.d(TAG, "onRequestPermissionsResult(): Granted!");
                getWeatherForCurrentLocation();
            }else{
                Log.d(TAG, "onRequestPermissionsResult: Permission Denied");
            }
        }
    }


    //create the networkingCalls method here
    //this method, we implement an HttpRequest, using it to make a Get request
    private void networkingCalls(RequestParams params){
        AsyncHttpClient client = new AsyncHttpClient();

        //the Json object handler is used to notify whether the Getrequest failed or was successful
        //the json response hanler receive 2 messages - onSucess and onFailure
        //both methods are declared below
        client.get(WEATHER_URL, params, new JsonHttpResponseHandler(){
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response){
                Log.d(TAG, "onSuccess: " + response.toString());
                // call the json from the WeatherDataModel
                WeatherDataModel weatherData = WeatherDataModel.fromJson(response);
                updateUI(weatherData);
            }

            @Override
            public void onFailure(int statuscode, Header[] headers, Throwable e, JSONObject response){
                Log.e(TAG, "onFailure: "+ e.toString());
                Log.d(TAG, "Status code: " + statuscode);
                Toast.makeText(WeatherActivity.this, "Request failed", Toast.LENGTH_SHORT).show();

            }

        });
    }

    private void updateUI(WeatherDataModel weather){
        mTemperature.setText(weather.getTemperature());
        mCity.setText(weather.getCity());

        int resourceID = getResources().getIdentifier(weather.getIconname(), "drawable", getPackageCodePath());
        mWeatherImage.setImageResource(resourceID);
    }

    //This code below frees up memory
    //when mListener is not in use and it is automatically generated
    @Override
    protected void onPause() {
        super.onPause();

        if(mLocationManager != null) mLocationManager.removeUpdates(mLocationListener);
    }
}

public class WeatherDataModel {

    private String mTemperature;
    private String mCity;
    private String mIconname;
    private int mCondition;


    //create a weatherdatamodel fromJson:
    public static  WeatherDataModel fromJson(JSONObject jsonObject) {
        WeatherDataModel weatherData = new WeatherDataModel();
        //we surround the json parsing code with a try-catch statement
        //to handle errors like nan and empty values
        try {
            //get json object called -id, that is nested oin an object "0", thats also nested in an array called weather
            weatherData.mCondition = jsonObject.getJSONArray("weather").getJSONObject(0).getInt("id");
            weatherData.mCity = jsonObject.getString("name");
            weatherData.mIconname = updateWeatherIcon(weatherData.mCondition);

            double temp = jsonObject.getJSONObject("main").getDouble("temp") - 273.15;

            int rdValue = (int) Math.rint(temp);

            weatherData.mTemperature= Integer.toString(rdValue);
            return weatherData;
        }catch (JSONException e){
            e.printStackTrace();

            return null;
        }
    }


    private static String updateWeatherIcon(int condition) {

        if (condition >= 0 && condition < 300) {
            return "tstorm1";
        } else if (condition >= 300 && condition < 500) {
            return "light_rain";
        } else if (condition >= 500 && condition < 600) {
            return "shower3";
        } else if (condition >= 600 && condition <= 700) {
            return "snow4";
        } else if (condition >= 701 && condition <= 771) {
            return "fog";
        } else if (condition >= 772 && condition < 800) {
            return "tstorm3";
        } else if (condition == 800) {
            return "sunny";
        } else if (condition >= 801 && condition <= 804) {
            return "cloudy2";
        } else if (condition >= 900 && condition <= 902) {
            return "tstorm3";
        } else if (condition == 903) {
            return "snow5";
        } else if (condition == 904) {
            return "sunny";
        } else if (condition >= 905 && condition <= 1000) {
            return "tstorm3";
        }

        return "dunno";
    }

    //create a Get() for the variable created, so it can be retrieved in the weatherActivity

    public String getTemperature() {
        return mTemperature + "°";
    }

    public String getCity() {
        return mCity;
    }

    public String getIconname() {
        return mIconname;
    }
}

2 个答案:

答案 0 :(得分:0)

首先尝试检查JSON对象是否存在:

public function store(Request $request)
{
    /*$this->validate(
        $request,
        [
            'name'=>'required|string|unique:books,book_name|max:30',
            'writer'=>'required|string|max:30',
            'publisher_name'=>'required|string|max:30',
            'publisher_date'=>'required',
            'description'=>'required',
            'pages_num'=>'required|integer|between:1,5000',
            'image'=>'required'
        ]
    );
    $user =new books;
    $user->book_name=Input::get("name");
    $user->writer=Input::get("writer");
    $user->publisher_name=Input::get("publisher_name");
    $user->publisher_date=Input::get("publisher_date");
    $user->description=Input::get("description");
    $user->pages_num=Input::get("pages_num");
    $user->image=input::get("image")
    $user->save();
}

答案 1 :(得分:0)

使用Firebase SDK 15.0.0(released April 10)构建后,我看到相同的堆栈跟踪,但消息略有不同:

org.json.JSONException: No value for userMetadata

对我来说,异常是非致命的,只会在第一次在设备上运行新版本时发生。我认为可以忽略它。

如果您反复看到错误或导致问题,请降级到Firebase 12.0.1。