Данные JSON возвращаются как нулевые при доступе к API OpenWeatherMap.

Переходим к Android и пытаемся следовать следующему руководству для приложения погоды, обновленного с API OpenWeatherMap http://code.tutsplus.com/tutorials/create-a-weather-app-on-android--cms-21587

В настоящее время у меня проблемы со следующим классом; объект JSON, возвращаемый при вызове метода getJSON(), всегда имеет значение null, в результате чего погода не отображается. URL-адрес работает, и я не уверен, что делаю неправильно; Я думаю, что у меня может быть какая-то проблема со строкой connection.addRequestProperty. Я не уверен, действительно ли ключ API требуется для этого анализа JSON, так как вы можете получить результаты в своем браузере без него, например. http://api.openweathermap.org/data/2.5/weather?q=London&units=metric

Если у кого-то есть понимание того, где я ошибаюсь, это будет очень признательно!

РЕДАКТИРОВАТЬ: если кому-то нужно увидеть остальную часть пакета, все в настоящее время идентично коду, содержащемуся в опубликованной ссылке.

РЕДАКТИРОВАТЬ 2: добавлен логарифм, созданный после изменений, предложенных dieter_h, спасибо, чувак.

ЛОГКАТ:

08-20 23:20:55.219  12169-12169/? I/art﹕ Late-enabling -Xcheck:jni
08-20 23:20:55.587  12169-12191/simpleweather.ockmore.will.simpleweather D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-20 23:20:55.598  12169-12169/simpleweather.ockmore.will.simpleweather D/Atlas﹕ Validating map...
08-20 23:20:55.659  12169-12188/simpleweather.ockmore.will.simpleweather I/myActivity﹕ data{"cod":"404","message":"Error: Not found city"}
08-20 23:20:55.671  12169-12191/simpleweather.ockmore.will.simpleweather I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020_msm8960_LA.AF.1.1_RB1__release_AU ()
OpenGL ES Shader Compiler Version: E031.25.03.06
Build Date: 03/30/15 Mon
Local Branch: mybranch8688311
Remote Branch: quic/LA.AF.1.1_rb1.16
Local Patches: NONE
Reconstruct Branch: AU_LINUX_ANDROID_LA.AF.1.1_RB1.05.00.02.006.020 + 9b2699f + 2215637 + 60aa592 + f2362e6 + 5c64f59 + 82411a1 + 1f36e07 +  NOTHING
08-20 23:20:55.673  12169-12191/simpleweather.ockmore.will.simpleweather I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-20 23:20:55.707  12169-12191/simpleweather.ockmore.will.simpleweather D/OpenGLRenderer﹕ Enabling debug mode 0
08-20 23:20:55.809  12169-12169/simpleweather.ockmore.will.simpleweather E/SimpleWeather﹕ One or more fields not found in JSON data

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

public class RemoteFetch {



public static JSONObject getJSON(Context context, String city){
    try {
        URL url = new URL("http://api.openweathermap.org/data/2.5/weather?q=" +city+ "&units=metric");
        HttpURLConnection connection =
                (HttpURLConnection)url.openConnection();

        connection.addRequestProperty("x-api-key",
                context.getString(R.string.open_weather_maps_app_id));

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));

        StringBuilder json = new StringBuilder(1024);
        String tmp=" ";
        while ((tmp=reader.readLine())!=null)
            json.append(tmp).append("\n");
        reader.close();

        JSONObject data = new JSONObject(json.toString());

        //This value will be 404 if the request was not
        //successful
        if (data.getInt("cod")!=200){
            return null;
        }

        return data;
    }catch(Exception e) {
        return null;
        }
    }
}

РЕДАКТИРОВАТЬ 3: ниже добавлены классы Fragment и Activity

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Handler;

import org.json.JSONObject;

import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;


public class WeatherFragment extends Fragment{
Typeface weatherFont;

TextView cityField;
TextView updatedField;
TextView detailsField;
TextView currentTemperatureField;
TextView weatherIcon;

Handler handler;

public WeatherFragment(){
    handler = new Handler();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_weather, container, false);
    cityField = (TextView)rootView.findViewById(R.id.city_field);
    updatedField = (TextView)rootView.findViewById(R.id.updated_field);
    detailsField = (TextView)rootView.findViewById(R.id.details_field);
    currentTemperatureField = (TextView)rootView.findViewById(R.id.current_temperature_field);
    weatherIcon = (TextView)rootView.findViewById(R.id.weather_icon);

    weatherIcon.setTypeface(weatherFont);
    return rootView;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    weatherFont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/weather.ttf");
    updateWeatherData(new CityPreference(getActivity()).getCity());
}

private void updateWeatherData(final String city){
    new Thread(){
        public void run(){
            final JSONObject json = RemoteFetch.getJSON(getActivity(), city);
            if (json == null){
                handler.post(new Runnable(){
                    public void run(){
                        Toast.makeText(getActivity(),
                                getActivity().getString(R.string.place_not_found),
                                Toast.LENGTH_LONG).show();
                    }
                });
            } else {
                handler.post(new Runnable(){
                    @Override
                    public void run() {
                        renderWeather(json);
                    }
                });
            }
        }
    }.start();
}

private void renderWeather(JSONObject json){
    try {
        cityField.setText(json.getString("name").toUpperCase(Locale.UK) +
                "," +
                json.getJSONObject("sys").getString("country"));

        JSONObject details = json.getJSONArray("weather").getJSONObject(0);
        JSONObject main = json.getJSONObject("main");
        detailsField.setText(
                details.getString("description").toUpperCase(Locale.UK) +
                        "\n" + "Humidity: " + main.getString("humidity") + "%" +
                        "\n" + "Pressure: " + main.getString("pressure") + "hPa");

        currentTemperatureField.setText(
                String.format("%.2f", main.getDouble("temp")) + " ℃");

        DateFormat df = DateFormat.getDateInstance();
        String updatedOn = df.format(new Date(json.getLong("dt")*1000));
        updatedField.setText("Last update: " + updatedOn);

        setWeatherIcon(details.getInt("id"),
                json.getJSONObject("sys").getLong("sunrise") * 1000,
                json.getJSONObject("sys").getLong("sunset") * 1000);
    }catch (Exception e) {
        Log.e("SimpleWeather", "One or more fields not found in JSON data");
    }
}

private void setWeatherIcon(int actualId, long sunrise, long sunset){
    int id = actualId / 100;
    String icon = "";
    if(actualId == 800){
        long currentTime = new Date().getTime();
        if (currentTime>=sunrise && currentTime<sunset) {
            icon = getActivity().getString(R.string.weather_sunny);
        } else {
            icon = getActivity().getString(R.string.weather_clear_night);
        }
    } else {
        switch (id) {
            case 2 : icon = getActivity().getString(R.string.weather_thunder);
                break;
            case 3 : icon = getActivity().getString(R.string.weather_drizzle);
                break;
            case 7 : icon = getActivity().getString(R.string.weather_foggy);
                break;
            case 8 : icon = getActivity().getString(R.string.weather_cloudy);
                break;
            case 6 : icon = getActivity().getString(R.string.weather_snowy);
                break;
            case 5 : icon = getActivity().getString(R.string.weather_rainy);
                break;
        }
        weatherIcon.setText(icon);
    }
}

public void changeCity(String city){
    updateWeatherData(city);
}

Формат немного странный, извините.

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.widget.EditText;

public class WeatherActivity extends AppCompatActivity {

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

    if(savedInstanceState == null){
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new WeatherFragment())
                .commit();
    }
}


@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_weather, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.change_city) {
        showInputDialog();
    }

    return false;
}

private void showInputDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Change city");
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_TEXT);
    builder.setView(input);
    builder.setPositiveButton("Go", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            changeCity(input.getText().toString());
        }
    });
    builder.show();
}

public void changeCity(String city){
    WeatherFragment wf = (WeatherFragment)getSupportFragmentManager()
                            .findFragmentById(R.id.container);
    wf.changeCity(city);
    new CityPreference(this).setCity(city);
    }
}

person Barnabus    schedule 20.08.2015    source источник
comment
Сообщение об ошибке Logcat...   -  person dieter_h    schedule 20.08.2015
comment
Ошибки нет, приложение просто не отображает фрагмент, связанный с данными о погоде. Основная активность по-прежнему загружается   -  person Barnabus    schedule 20.08.2015
comment
Есть ли шанс, что вы можете включить классы Activity и fragment?   -  person Want2bExpert    schedule 20.08.2015
comment
Я опубликую их ниже класса RemoteFetch в вопросе :)   -  person Barnabus    schedule 21.08.2015


Ответы (3)


Попробуйте это, добавьте это после цикла while.

finally
{
try {
if (reader != null)
reader.close();
}// end try
catch (IOException ex)
{
}// end catch 
}//end finally.
person Want2bExpert    schedule 20.08.2015

Измените код и опубликуйте свой logcat:

      JSONObject data = new JSONObject(json.toString());

      Log.i("MyActivity", "data + " data);
      [...]

      return data;
    }catch(Exception e) {
        Log.e("MyActivity", "Exception", e.fillInStackTrace());
        return null;
}
person dieter_h    schedule 20.08.2015

Пожалуйста, используйте addRequestProperty для setRequestProperty:

connection.setRequestProperty("x-api-key", context.getString(R.string.open_weather_maps_app_id));

вместо:

connection.addRequestProperty("x-api-key", context.getString(R.string.open_weather_maps_app_id));

person Actiwitty    schedule 20.08.2015