При отладке я получаю это
После строки 'httpURLConnection.connect ();'мой код идет в блок catch и выдает мне следующие выходные данные:
output catch part 2
Я знаю, что есть несколько сообщений на эту темуно я, кажется, не могу найти решение этой проблемы.Я не понимаю, почему я получаю эту ошибку.Я знаю, что, скорее всего, получаю нулевой объект из моего метода doInbackground (), но я не понимаю, почему.Может ли кто-нибудь помочь мне понять это, чтобы я знал, как это исправить.Спасибо !
Мой код:
private class PlaceRequest extends AsyncTask<String, Integer, JSONArray> {
@Override
protected JSONArray doInBackground(String... params) {
try {
URL url=new URL(params[0]);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.connect();
String line;
StringBuilder stringBuilder = new StringBuilder("");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
JSONObject jsonObject=new JSONObject(stringBuilder.toString());
if (jsonObject.has("next_page_token")) {
nextPageToken =jsonObject.getString("next_page_token");
} else {
nextPageToken="";
}
return jsonObject.getJSONArray("results");
} catch (Exception e) {
e.printStackTrace();
}
return new JSONArray();
}
@Override
protected void onPostExecute(JSONArray jsonArray) {
progressBar.setVisibility(View.GONE);
requestCount++;
try {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
JSONObject location = jsonObject.getJSONObject("geometry").getJSONObject("location");
String placeId = jsonObject.getString("place_id");
String name = jsonObject.getString("name");
LatLng latLng = new LatLng(location.getDouble("lat"), location.getDouble("lng"));
GooglePlace googlePlace = new GooglePlace(name, placeId, latLng);
listPlaces.add(googlePlace);
MarkClusterItem markerClusterItem = new MarkClusterItem(latLng, name);
clusterManager.addItem(markerClusterItem);
}
clusterManager.cluster();
} catch (Exception e) {
e.printStackTrace();
}
if (requestCount < REQUEST_LIMIT && !nextPageToken.equals("")) {
progressBar.setVisibility(View.VISIBLE);
String url=PLACES_REQUEST+"&pagetoken="+nextPageToken;
new Handler().postDelayed(() -> new PlaceRequest().execute(url), 2000);
}
}
}
}
ошибка:
2018-12-09 23:59:51.168 13210-13210/com.example.flow E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.flow, PID: 13210
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at com.example.flow.displayClasses.WebscrapingScreens.GoogleMapsFragment$PlaceRequest.onPostExecute(GoogleMapsFragment.java:271)
at com.example.flow.displayClasses.WebscrapingScreens.GoogleMapsFragment$PlaceRequest.onPostExecute(GoogleMapsFragment.java:218)
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)
2018-12-09 23:59:51.204 1867-1881/? E/memtrack: Couldn't load memtrack module
2018-12-09 23:59:51.329 1867-1935/? E/InputDispatcher: channel '7cc9433 com.example.flow/com.example.flow.displayClasses.LoginScreens.Login (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-12-09 23:59:51.329 1867-1935/? E/InputDispatcher: channel 'a9e800c com.example.flow/com.example.flow.Home (server)' ~ Channel is unrecoverably broken and will be disposed!
2018-12-09 23:59:52.005 1711-2262/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2018-12-09 23:59:52.059 1711-2050/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
2018-12-09 23:59:54.394 1867-1983/? E/TaskPersister: File error accessing recents directory (directory doesn't exist?).
2018-12-09 23:59:57.468 5475-5549/? E/PlayCommon: [332] com.google.android.play.b.g.a(448): Failed to connect to server: java.net.UnknownHostException: Unable to resolve host "play.googleapis.com": No address associated with hostname
2018-12-09 23:59:58.360 1867-1881/? E/memtrack: Couldn't load memtrack module
2018-12-09 23:59:58.362 1867-1881/? E/memtrack: Couldn't load memtrack module
2018-12-09 23:59:58.362 1867-1881/? E/memtrack: Couldn't load memtrack module
Манифест:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.flow">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="Eazy-Split"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
tools:ignore="GoogleAppIndexingWarning"
tools:replace="android:icon,android:label"
>
<uses-library android:name="org.apache.http.legacy" android:required="false" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyApsuynhkRf3A7p3fgKQp01EEF8l4tggXQ" />
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Home"
android:parentActivityName=".displayClasses.LoginScreens.Login"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".displayClasses.LoginScreens.Login" />
</activity>
<activity
android:name=".displayClasses.LoginScreens.activity_signup"
android:theme="@style/AppTheme"
/>
<activity
android:name=".displayClasses.LoginScreens.Login"
android:theme="@style/AppTheme"
/>
</application>
</manifest>