java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 296


先貼代碼

public class ListDataRequest extends JsonRequest<ArrayList<Note>> {

 public ListDataRequest(int method, String url,Map<String,String> contentBody, Listener<ArrayList<Note>> listener, ErrorListener errorlistener) {
  super(method, url, contentBody, listener, errorlistener);
 }

// public ListDataRequest(String url, Map requestBody, Listener<ArrayList> listener,ErrorListener errorListener){
//  super(url, requestBody, listener, errorListener);
// }

 @Override
 protected Response<ArrayList<Note>> parseNetworkResponse(NetworkResponse response) {
  try {
   return Response.success(parseStrToNoteArray(new String(response.data,
       HttpHeaderParser.parseCharset(response.headers))),
     HttpHeaderParser.parseCacheHeaders(response));
  } catch (UnsupportedEncodingException e) {
   return Response.error(new ParseError(e));
  }
 }
 
 private ArrayList<Note> parseStrToNoteArray(String resultStr) {
  Gson gson = new Gson();
  JsonParser jsonParse = new JsonParser();
  JsonObject rootObj = jsonParse.parse(resultStr).getAsJsonObject();
  ArrayList<Note> array = null;
  try {
   // parseResult(rootObj.getAsJsonObject("root"));
   String json = gson.toJson(rootObj.getAsJsonArray("msg"));
//   Log.i("json", json);
   array = gson.fromJson(gson.toJson(rootObj.getAsJsonArray("msg")),new TypeToken<ArrayList<Note>>() {}.getType());
  } catch (JsonSyntaxException e) {
   e.printStackTrace();
  }
  return array;
 } 

}

 array = gson.fromJson(gson.toJson(rootObj.getAsJsonArray("msg")),new TypeToken<ArrayList<Note>>() {}.getType());

這行代碼反復出現 java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 296錯誤

我就在想啊這地方完全正確啊,怎么你想要一個String  我卻給你了個array,怎么回事啊,我盯着這行代碼看了一天半,是現在找不到哪里錯了,剛開始做了一個demo,把ArrayList里的泛型去掉了,就能正確獲取數據,我就在想難道是我的bean錯了,我看了看人家的demo,也是一樣啊,怎么我的就不行啊,最后還是一個哥給我指出了錯誤

貼出我的note

 

package com.blue.bean;

import java.util.ArrayList;

import org.json.JSONArray;

import com.google.gson.annotations.SerializedName;

/**
 * @author SLJM
 * @create 2014-4-26
 * @desc 帖子(note)或文章(article)
 *
 */

public class Note {
 /**
  * 帖子ID
  */
 @SerializedName("id")
 public String id;
 /**
  * 用戶ID
  */
 @SerializedName("user_id")
 public String user_id;
 /**
  * 評論個數
  */
 @SerializedName("comment_count")
 public String comment_count;
 /**
  *  創建時間
  */
 @SerializedName("data_create")
 public String ctime;
 /**
  * 手機型號
  */
 @SerializedName("phone_type")
 public String phone_type;
 /**
  * 轉發個數
  */
 @SerializedName("re_count")
 public String re_count;
 
 /**
  * 帖子類型
  *1:帖子2:單頁 3:新聞4:資訊  5:轉發帖
  */
 @SerializedName("type")
 public String type;
 
 
 /**
  * 帖子內容
  */
 @SerializedName("info")
 public String content;
 /**
  * 用戶昵稱
  */
 @SerializedName("user_name")
 public String user_name;
 /**
  * 文章標題(只在行業資訊和文章列表中顯示,大咖秀沒有)
  */
 @SerializedName("title")
 public String title;
 /**
  *  贊個數
  */
 @SerializedName("like_count")
 public String like_count;
 /**
  *  圖片數組
  */
 @SerializedName("img")
 public ArrayList<NotePhoto> img;
 
}

 

這地方img,服務器給的數據是一個Array,我返回的是String,就是這個斜體的地方,我以前是String,人家返回的是一個數組,我卻用String接收,當然要報錯了,我還一直釘在那行代碼看,真是無語啊,弄了一天半,郁悶啊,遇到問題,一定要解決,不要總想着逃過去,我之前也是想那么做,可是他會一直跟着你,菜鳥,繼續走吧,我們一直在路上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM