2016-02-24 1 views
0

Это мой JSONAndroid Bug: Ожидаемое BEGIN_OBJECT но BEGIN_ARRAY в строке 1 колонки 2 пути в андроида

[ 
    { 
     "id":1, 
     "media":{ 
      "name":"ABC", 
      "url":"abc.org/" 
     }, 
     "published":"2016-01-24T16:00:00.000Z", 
     "_links":{ 
      "self":{ 
       "href":"acb.net" 
      } 
     } 
    } 
] 

ApiInterface Класс

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; 

Класс ApiService

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; 
    } 

Это моя ошибка.

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $ 

at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:200) 
                      at com.google.gson.Gson.fromJson(Gson.java:810) 
                      at com.seesaa.newsaudiocast.api.CleanGsonConverter.fromBody(CleanGsonConverter.java:60) 
                      at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:367) 
                      at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240) 
                      at $Proxy0.getAllApiFeedCurrent(Native Method) 
                      at com.seesaa.newsaudiocast.api.ApiService.getAllData(ApiService.java:50) 
                      at com.seesaa.newsaudiocast.activity.SplashActivity$GetDataAsync.doInBackground(SplashActivity.java:75) 
                      at com.seesaa.newsaudiocast.activity.SplashActivity$GetDataAsync.doInBackground(SplashActivity.java:49) 
                      at android.os.AsyncTask$2.call(AsyncTask.java:288) 

Просьба. Помогите мне исправить ошибку. Спасибо всем!

ответ

0

Класс ApiInterface

public interface ApiServiceInterface { 
    @GET("/api/feed/channels/current/entries") 
    ApiFeedCurrentRequest getAllApiFeedCurrent(); 
} 

заменить

public interface ApiServiceInterface { 
    @GET("/api/feed/channels/current/entries") 
    List<ApiFeedCurrentRequest> 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; 
     } 

Надежда это поможет вам, мой друг! :)

+0

Это сработало! Большое спасибо и самое лучшее! :) –

+0

Вы всегда приветствуете моего друга! – Rahul

0

изменения этого

ApiFeedCurrentRequest getAllApiFeedCurrent(); 

к этому ApiFeedCurrentRequest [] getAllApiFeedCurrent();

или

List<ApiFeedCurrentRequest> getAllApiFeedCurrent(); 
+0

Да. Он попробует! Большое вам спасибо! –

+0

Это сработало! Большое спасибо и самое лучшее! –

Смежные вопросы