Я использую этот URL:
https://jsonplaceholder.typicode.com/posts
Я хочу восстановить заголовок и тело этого json, но когда я передаю
JSONArray postArrayJson = jsonObject.getJSONArray("");
Это не работа ...
Я использую эту функцию:
private List<Post> getPosts(JSONObject jsonObject) throws JSONException {
List<Post> posts = new ArrayList<>();
JSONArray postArrayJson = jsonObject.getJSONArray("");
for (int i = 0; i < postArrayJson.length(); i++) {
JSONObject postArrayJsonJSONObject = postArrayJson.getJSONObject(i);
String title = postArrayJsonJSONObject.getString("title");
String body = postArrayJsonJSONObject.String("body");
Post postsObj = new Post();
postsObj.setTitle(body);
postsObj.setBody(title);
posts.add(postsObj);
}
return posts;
}
Я возвращаю ее сюда:
JSONObject jsonObject = new JSONObject(jsonAsString);
List<Post> posts = getPosts(jsonObject);
Весь код:
public class JsonPostTask extends AsyncTask<String, Void, List<Post>> {
private final WeakReference<Context> context;
private ProgressDialog dialog;
private PostLoader postLoader;
private BufferedInputStream is;
public JsonPostTask(Context context) {
this.context = new WeakReference<>(context);
}
public void setPostLoader(PostLoader postLoader) {
this.postLoader = postLoader;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Context context = this.context.get();
if(context != null)
dialog = ProgressDialog.show(context, "Carregando", "", true);
}
@Override
protected List<Post> doInBackground(String... params) {
String url = params[0];
try {
URL requestUrl = new URL(url);
HttpsURLConnection urlConnection = (HttpsURLConnection) requestUrl.openConnection();
urlConnection.setReadTimeout(2000);
urlConnection.setConnectTimeout(2000);
int responseCode = urlConnection.getResponseCode();
if (responseCode > 400)
throw new IOException("Erro na comunicação com o servidor remoto");
InputStream inputStream = urlConnection.getInputStream();
is = new BufferedInputStream(urlConnection.getInputStream());
String jsonAsString = toString(is);
JSONArray jsonObject = new JSONArray(jsonAsString);
List<Post> posts = getPosts();
is.close();
return posts;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private List<Post> getPosts() throws JSONException, IOException {
String jsonAsString = toString(is);
JSONArray postArrayJson = new JSONArray(jsonAsString);
List<Post> posts = new ArrayList<>();
for (int i = 0; i < postArrayJson.length(); i++) {
JSONObject postArrayJsonJSONObject = postArrayJson.getJSONObject(i);
String title = postArrayJsonJSONObject.getString("title");
String body = postArrayJsonJSONObject.getString("body");
Post postsObj = new Post();
postsObj.setTitle(body);
postsObj.setBody(title);
posts.add(postsObj);
}
return posts;
}
private String toString(InputStream inputStream) throws IOException {
byte[] bytes = new byte[1024];
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int alreadyRead;
while ((alreadyRead = inputStream.read(bytes)) > 0) {
byteArrayOutputStream.write(bytes, 0, alreadyRead);
}
return new String(byteArrayOutputStream.toByteArray());
}
@Override
protected void onPostExecute(List<Post> posts) {
super.onPostExecute(posts);
dialog.dismiss();
if (postLoader != null)
postLoader.onResult(posts);
}
}