Android错误:预期为BEGIN_对象,但在Android中的第1行第2列路径处为BEGIN_数组
这是我的JSONAndroid错误:预期为BEGIN_对象,但在Android中的第1行第2列路径处为BEGIN_数组,android,json,serialization,Android,Json,Serialization,这是我的JSON [ { "id":1, "media":{ "name":"ABC", "url":"abc.org/" }, "published":"2016-01-24T16:00:00.000Z", "_links":{ "self":{ "href":"acb.net"
[
{
"id":1,
"media":{
"name":"ABC",
"url":"abc.org/"
},
"published":"2016-01-24T16:00:00.000Z",
"_links":{
"self":{
"href":"acb.net"
}
}
}
]
类接口
public interface ApiServiceInterface {
@GET("/api/feed/channels/current/entries")
ApiFeedCurrentRequest getAllApiFeedCurrent();
}
类ApiFeedCurrentRequest
public class ApiFeedCurrentRequest {
@SerializedName("id")
private int mId;
@SerializedName("media")
private Media mMedia;
@SerializedName("published")
private String mPublished;
@SerializedName("_links")
private Link mLinks;
班级服务
private static final String TAG = "__API__Service";
private final ApiServiceInterface mApiService;
public ApiService(Context context) {
final OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.setReadTimeout(30, TimeUnit.SECONDS);
okHttpClient.setConnectTimeout(30, TimeUnit.SECONDS);
Gson gson = new GsonBuilder()
.setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constant.BASE_URL)
.setLogLevel(RestAdapter.LogLevel.FULL)
.setClient(new OkClient(okHttpClient))
.setLogLevel(BuildConfig.DEBUG ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE)
.setLog(new AndroidLog(TAG))
.setConverter(new CleanGsonConverter(gson))
.setErrorHandler(new CustomErrorHandler(context))
.build();
this.mApiService = restAdapter.create(ApiServiceInterface.class);
}
public ApiFeedCurrentRequest getAllData() {
if (mApiService != null) {
return mApiService.getAllApiFeedCurrent();
} else {
return null;
}
}
Class CleanGsonConverter
public class CleanGsonConverter extends GsonConverter {
private Gson mGson;
public CleanGsonConverter(Gson gson) {
super(gson);
mGson = gson;
}
public CleanGsonConverter(Gson gson, String encoding) {
super(gson, encoding);
mGson = gson;
}
@Override
public Object fromBody(TypedInput body, Type type) throws ConversionException {
boolean willCloseStream = false; // try to close the stream, if there is no exception thrown using tolerant JsonReader
try {
String mDirty = toString(body);
if (TextUtils.isEmpty(mDirty)) return null;
String clean = mDirty.replaceAll("(^\\(|\\)$)", "");
body = new JsonTypedInput(clean.getBytes(Charset.forName("UTF-8")));
JsonReader jsonReader = new JsonReader(new InputStreamReader(body.in()));
jsonReader.setLenient(true);
Object o = mGson.fromJson(jsonReader, type);
willCloseStream = true;
return o;
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
if (willCloseStream) {
closeStream(body);
}
}
}
private String toString(TypedInput body) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(body.in()));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
private void closeStream(TypedInput body) {
try {
InputStream in = body.in();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在活动中
private class GetDataAsync extends AsyncTask<Void, Void, Void> {
private WeakReference<SplashActivity> mWeakReference;
private ProgressDialog mDialog;
private boolean mErrorInternet = false;
private ApiFeedCurrentRequest mApiFeedCurrent;
public GetDataAsync(SplashActivity splashActivity) {
mWeakReference = new WeakReference<SplashActivity>(splashActivity);
mDialog = new ProgressDialog(splashActivity);
mDialog.setMessage(splashActivity.getString(R.string.message_loading));
mDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
SplashActivity activity = mWeakReference.get();
if (activity != null) {
if (Utils.isInternetAvailable()) {
try {
mApiFeedCurrent = activity.mApiService.getAllData();
} catch (RetrofitError error) {
DebugTool.logD("ERROR = " + error.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
mErrorInternet = true;
}
}
return null;
}
求你了。帮我修正错误。谢谢大家 类API接口
public interface ApiServiceInterface {
@GET("/api/feed/channels/current/entries")
ApiFeedCurrentRequest getAllApiFeedCurrent();
}
取代
公共接口ApiServiceInterface{
@获取(“/api/feed/channels/current/entries”)
列出getAllApiFeedCurrent();
}
和:在活动中
private List<ApiFeedCurrentRequest> mApiFeedCurrent = new ArrayList<>();
@Override
protected Void doInBackground(Void... params) {
SplashActivity activity = mWeakReference.get();
if (activity != null) {
if (Utils.isInternetAvailable()) {
try {
mApiFeedCurrent = activity.mApiService.getAllData();
} catch (RetrofitError error) {
DebugTool.logD("ERROR = " + error.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
mErrorInternet = true;
}
}
return null;
}
private List mApiFeedCurrent=new ArrayList();
@凌驾
受保护的Void doInBackground(Void…参数){
SplashActivity活动=mWeakReference.get();
if(活动!=null){
if(Utils.isInternetAvailable()){
试一试{
mApiFeedCurrent=activity.mApiService.getAllData();
}捕获(错误){
DebugTool.logD(“ERROR=“+ERROR.toString());
}捕获(例外e){
e、 printStackTrace();
}
}否则{
mErrorInternet=true;
}
}
返回null;
}
希望它能帮助你,我的朋友!:) 改变这个
ApiFeedCurrentRequest getAllApiFeedCurrent();
对此
ApiFeedCurrentRequest[]getAllApiFeedCurrent()
或
List getAllApiFeedCurrent();
是。它会试试的!非常感谢!
private List<ApiFeedCurrentRequest> mApiFeedCurrent = new ArrayList<>();
@Override
protected Void doInBackground(Void... params) {
SplashActivity activity = mWeakReference.get();
if (activity != null) {
if (Utils.isInternetAvailable()) {
try {
mApiFeedCurrent = activity.mApiService.getAllData();
} catch (RetrofitError error) {
DebugTool.logD("ERROR = " + error.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
mErrorInternet = true;
}
}
return null;
}
ApiFeedCurrentRequest getAllApiFeedCurrent();
List<ApiFeedCurrentRequest> getAllApiFeedCurrent();