Получение JSON с применением API
Добрый день, пишу, так как не могу понять в чем проблема.
Не могу в приложении получить JSONObject, потом начал писать код так же, как и в видеоуроке, но все равное не получается,
приложение просто сыпется.
API передаю корректный.
public class MainActivity extends AppCompatActivity {
private final String WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather?q=Moscow&appid=ce12d17aeef642f58956750e47c69285&lang=ru&units=metric";
private EditText editText;
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editTextTextPersonName);
button = findViewById(R.id.buttonShowWeather);
textView = findViewById(R.id.textViewWeather);
}
public void onClickGetWeather(View view) {
DownloadWeatherTask task = new DownloadWeatherTask();
task.execute(WEATHER_URL);
}
private class DownloadWeatherTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... strings) {
StringBuilder result = new StringBuilder();
URL url = null;
HttpURLConnection urlConnection = null;
try {
url = new URL(strings[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line = bufferedReader.readLine();
while(line != null){
result.append(line);
line = bufferedReader.readLine();
}
return result.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(urlConnection != null){
urlConnection.disconnect(); }
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
try {
JSONObject jsonObject = new JSONObject(s);
String city = jsonObject.getJSONObject("coord").getString("lon");
textView.setText(city);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
А вот и сама ошибка:
--------- beginning of crash
2021-03-26 08:52:31.598 4406-4406/com.example.myweatherapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myweatherapp, PID: 4406
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
at org.json.JSONTokener.nextValue(JSONTokener.java:94)
at org.json.JSONObject.<init>(JSONObject.java:159)
at org.json.JSONObject.<init>(JSONObject.java:176)
at com.example.myweatherapp.MainActivity$DownloadWeatherTask.onPostExecute(MainActivity.java:78)
at com.example.myweatherapp.MainActivity$DownloadWeatherTask.onPostExecute(MainActivity.java:45)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.access$600(AsyncTask.java:180)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
2021-03-26 08:52:32.070 4406-4406/com.example.myweatherapp I/Process: Sending signal. PID: 4406 SIG: 9