我是新来的,我的代码有问题而且我不知道它有什么问题,因为四个月前我创建的代码很完美,但知道我想继续该程序,我的代码也没有不再工作了。
因此,我的代码的目的是获取布鲁塞尔漫画书绘画的所有信息,这些信息都是JSON形式,来自开放数据布鲁塞尔的Api。
我的代码分为两个一级异步任务和一个输入流操作的类: 这是我使用的api的网址:https://opendata.bruxelles.be/api/v2/catalog/datasets/bruxelles_parcours_bd/records?start=0&stop=52
首先是模型类:
package com.formation.appli.brusselscomicbookpaintingtourapp.models;
import android.os.Parcel;
import android.os.Parcelable;
/**
* Created by Jeremyvdm on 27/10/2017.
*/
public class ComicBookPainting implements Parcelable{
String comicsPaintingTitle = "";
String comicsPaintingAuthor = "";
int comicsPaintingYear = 0;
double lat = 0.0;
double lng = 0.0;
String comicBookPaintingImageURL = "";
public ComicBookPainting(){
}
public ComicBookPainting(String comicsPaintingTitle, String comicsPaintingAuthor, int comicsPaintingYear, double lat, double lng, String comicBookPaintingImageURL){
this.comicsPaintingTitle = comicsPaintingTitle;
this.comicsPaintingAuthor = comicsPaintingAuthor;
this.comicsPaintingYear = comicsPaintingYear;
this.lat = lat;
this.lng = lng;
this.comicBookPaintingImageURL = comicBookPaintingImageURL;
}
public String getComicsPaintingTitle() {
return comicsPaintingTitle;
}
public void setComicsPaintingTitle(String comicsPaintingTitle) {
this.comicsPaintingTitle = comicsPaintingTitle;
}
public String getComicsPaintingAuthor() {
return comicsPaintingAuthor;
}
public void setComicsPaintingAuthor(String comicsPaintingAuthor) {
this.comicsPaintingAuthor = comicsPaintingAuthor;
}
public int getComicsPaintingYear() {
return comicsPaintingYear;
}
public void setComicsPaintingYear(int comicsPaintingYear) {
this.comicsPaintingYear = comicsPaintingYear;
}
public double getLat() {
return lat;
}
public void setLat(double lat) {
this.lat = lat;
}
public double getLng() {
return lng;
}
public void setLng(double lng) {
this.lng = lng;
}
public String getComicBookPaintingImageURL() {
return comicBookPaintingImageURL;
}
public void setComicBookPaintingImageURL(String comicBookPaintingImageURL) {
this.comicBookPaintingImageURL = comicBookPaintingImageURL;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int i) {
dest.writeString(comicsPaintingTitle);
dest.writeString(comicsPaintingAuthor);
dest.writeInt(comicsPaintingYear);
dest.writeDouble(lat);
dest.writeDouble(lng);
dest.writeString(comicBookPaintingImageURL);
}
protected ComicBookPainting(Parcel in) {
comicsPaintingTitle = in.readString();
comicsPaintingAuthor = in.readString();
comicsPaintingYear = in.readInt();
lng = in.readDouble();
lat = in.readDouble();
comicBookPaintingImageURL = in.readString();
}
public static final Creator<ComicBookPainting> CREATOR = new Creator<ComicBookPainting>() {
@Override
public ComicBookPainting createFromParcel(Parcel in) {
return new ComicBookPainting(in);
}
@Override
public ComicBookPainting[] newArray(int size) {
return new ComicBookPainting[size];
}
};
}
这是我的Asynch任务:
package com.formation.appli.brusselscomicbookpaintingtourapp.asynch;
import android.os.AsyncTask;
import android.util.Log;
import com.formation.appli.brusselscomicbookpaintingtourapp.models.ComicBookPainting;
import com.formation.appli.brusselscomicbookpaintingtourapp.tools.InputStreamOperations;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
/**
* Created by Jeremyvdm on 08/11/2017.
*/
public class GetTheComicBookPaintingFromJson extends AsyncTask<Integer, Void, ArrayList<ComicBookPainting>> {
// déclaration des variables
private ArrayList<ComicBookPainting> completeListOfComicBookPainting;
private int debut = 0;
private int fin = 52;
//region Callback
public interface GetTheComicBookPaintingFromJsonCallBack {
void listOfComicBookPainting( ArrayList<ComicBookPainting> completeListOfComicBookPainting);
}
private GetTheComicBookPaintingFromJsonCallBack callback;
public void setCallback(GetTheComicBookPaintingFromJsonCallBack callback) {
this.callback = callback;
}
//endregion
// déclaration de l'url en partie sous forme de constante
private static final String URLBASE_PARCOURT_BD = "http://opendata.bruxelles.be/api/v2/catalog/datasets/bruxelles_parcours_bd/records?";
private static final String URL_PARCOURT_START = "start=";
private static final String URL_PARCOURT_STOP = "stop=";
//initalisation du callback
@Override
protected void onPostExecute(ArrayList<ComicBookPainting> fresqueBDs) {
if(callback!=null){
callback.listOfComicBookPainting(completeListOfComicBookPainting);
}
}
//récupération du parcourt sous forme de ArrayList<FresqueBD>
@Override
protected ArrayList<ComicBookPainting> doInBackground(Integer... integers) {
completeListOfComicBookPainting = new ArrayList<>();
String brusselsComicsPaintingURL = URLBASE_PARCOURT_BD + URL_PARCOURT_START + debut + "&" + URL_PARCOURT_STOP + fin;
Log.v("TEST_URI_API",brusselsComicsPaintingURL);
JSONObject brusselsComicsPaintingJSON = resuqestJson(brusselsComicsPaintingURL);
JSONArray brusselsComicsPaintingJSONArray = listOfComicBookPaintingJsonArray(brusselsComicsPaintingJSON);
completeListOfComicBookPainting = convertJsorrayTolistOfComicBookPainting(brusselsComicsPaintingJSONArray);
return completeListOfComicBookPainting;
}
// Transformation du Json en fresque bd
private ComicBookPainting convertJsonToComicBookPainting(JSONObject jsonFresqueBD) {
String title = "";
String author = "";
int year = 0;
double lon =0;
double lat = 0;
String imageURL = "";
try {
JSONObject jsonField = jsonFresqueBD.getJSONObject("record").optJSONObject("fields");
title = jsonField.getString("personnage_s");
author = jsonField.getString("auteur_s");
year = Integer.parseInt(jsonField.getString("year"));
JSONObject jsonCoordonees = jsonField.getJSONObject("coordonnees_geographiques");
lon = jsonCoordonees.getDouble("lon");
lat = jsonCoordonees.getDouble("lat");
JSONObject JsonPhoto = jsonField.getJSONObject("photo");
imageURL = JsonPhoto.getString("url");
} catch (JSONException e) {
e.printStackTrace();
}
return new ComicBookPainting(title,author,year,lat,lon,imageURL);
}
// transformation du json objet en json array
public JSONArray listOfComicBookPaintingJsonArray(JSONObject jsonComicBookPaintingArray){
JSONArray listOfComicBookPaintingJsonArray = new JSONArray();
try {
listOfComicBookPaintingJsonArray = jsonComicBookPaintingArray.getJSONArray("records");
} catch (JSONException e) {
e.printStackTrace();
}
return listOfComicBookPaintingJsonArray;
}
// Transformation du JsonArray en ArrayList<fresqueBD>
public ArrayList<ComicBookPainting> convertJsorrayTolistOfComicBookPainting(JSONArray jsonArray){
int lenghtParcourtJson = jsonArray.length();
ArrayList<ComicBookPainting> completeListOfComicBookPaintings = new ArrayList<>();
for(int i =0; i < lenghtParcourtJson;i++) {
String comicBookPaintingUrl = null;
try {
comicBookPaintingUrl = jsonArray.getJSONObject(i).getJSONArray("links").getJSONObject(0).getString("href");
URL comicBookPaintingURL = new URL(comicBookPaintingUrl);
HttpURLConnection connection = (HttpURLConnection) comicBookPaintingURL.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
JSONObject jsonFresqueBD = new JSONObject(InputStreamOperations.InputStreamToString(inputStream));
ComicBookPainting comicBookPainting = convertJsonToComicBookPainting(jsonFresqueBD);
completeListOfComicBookPaintings.add(comicBookPainting);
} catch (JSONException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return completeListOfComicBookPaintings;
}
public JSONObject resuqestJson(String urlString){
JSONObject json=null;
HttpURLConnection connection = null;
try {
URL url = new URL(urlString);
connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = connection.getInputStream();
String result = InputStreamOperations.InputStreamToString(inputStream);
json = new JSONObject(result);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}finally {
connection.disconnect();
}
return json;
}
}
这是我的最后一个类输入流操作(我认为问题出在这个文件中,因为缓冲区读取器总是空的,我知道我的JSON工作,因为我使用api为我的iPhone应用程序,它完美地工作):
package com.formation.appli.bruxellesparcourbd.tools;
import java.io.IOException;
import java.io.InputStream;
/**
* Created by Jeremyvdm on 03/07/2017.
*/
//traite les String afin de les transformer en Json
public class InputStreamOperations {
public static String InputStreamToString (InputStream in, int bufSize) {
final StringBuilder out = new StringBuilder();
final byte[] buffer = new byte[bufSize];
try {
for (int ctr; (ctr = in.read(buffer)) != -1;) {
out.append(new String(buffer, 0, ctr));
}
} catch (IOException e) {
throw new RuntimeException("Cannot convert stream to string", e);
}
// On retourne la chaine contenant les donnees de l'InputStream
return out.toString();
}
/**
* @param in : buffer with the php result
* @return : the string corresponding to the buffer
*/
public static String InputStreamToString (InputStream in) {
// On appelle la methode precedente avec une taille de buffer par defaut
return InputStreamToString(in, 1024);
}
}
我不知道我的代码有什么问题我尝试了几件事,但我真的需要你的帮助。
提前感谢您的时间。
祝你好运
杰里米