2017-08-05 73 views
1

在MVP結構中構建我的應用程序時,我遇到了一個問題,當我使用Retrofit調用Web服務以成功響應時 我將整個數據傳入json字符串,這會激發我去做這個。gson中的java.lang.StackOverflowError

我的日誌貓:

FATAL EXCEPTION: main java.lang.StackOverflowError 
    at java.lang.AbstractStringBuilder.append0(AbstractStringBuilder.java:147) 
    at java.lang.StringBuffer.append(StringBuffer.java:219) 
    at java.io.StringWriter.write(StringWriter.java:147) 
    at java.io.StringWriter.append(StringWriter.java:199) 
    at java.io.StringWriter.append(StringWriter.java:30) 
    at com.google.gson.stream.JsonWriter.beforeValue(JsonWriter.java:651) 
    at com.google.gson.stream.JsonWriter.open(JsonWriter.java:325) 
    at com.google.gson.stream.JsonWriter.beginObject(JsonWriter.java:308) 
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:205) 
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145) 
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) 
    at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107) 
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) 
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:208) 
    at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.write(MapTypeAdapterFactory.java:145) 
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.write(ReflectiveTypeAdapterFactory.java:125) 
    at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:243) 
    at com.google.gson.internal.bind.ObjectTypeAdapter.write(ObjectTypeAdapter.java:107) 
    at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.write(TypeAdapterRuntimeTypeWrapper.java:69) 

這是我改造API調用:

//API Call for Pitch 
public void pitch() { 
    services 
     .getAPI() 
     .pitchList() 
     .enqueue(new Callback<PitchList_Res>() { 
      @Override 
      public void onResponse(Call<PitchList_Res> call, Response<PitchList_Res> response) { 
       if (!response.body().getdATA().isEmpty()) { 
        Gson gson1=new Gson(); 
        String json = gson1.toJson(response); 
        mListener.pitch(json); 
       } 
      } 

      @Override 
      public void onFailure(Call<PitchList_Res> call, Throwable t) { 
       call.cancel(); 
       Toast.makeText(context, R.string.error, Toast.LENGTH_SHORT).show(); 
      } 
     }); 
} 

這裏是主要活動在那裏我得到了響應:

//Response of Pitch 
@Override 
public void pitch(String response_pitch) { 

    editor.putString(Preference_Data.PITCH_RESPONSE, response_pitch); 
    editor.commit(); 
    PitchList_Res pitchList_res = gson.fromJson(response_pitch, PitchList_Res.class); 
    pitchlist.addAll(pitchList_res.getdATA()); 

    for (int i = 0; i < pitchlist.size(); i++) { 

     View pitchview = getLayoutInflater().inflate(
       R.layout.textviewlayout, null); 
     final TextView tvtitle = (TextView) pitchview 
       .findViewById(R.id.tv_title); 

     String id = pitchlist.get(i).getpITCHTYPEID().toString(); 
     tvtitle.setId(Integer.valueOf(id)); 

     tvtitle.setText(pitchlist.get(0).getpITCHSHORT().toString()); 
     tvtitle.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       if (stPitchSelectionID != -1) { 
        findViewById(stPitchSelectionID) 
          .setBackgroundResource(
            R.drawable.btn_bg); 
       } 
       if (stPitchSelectionID == v.getId()) { 
        stPitchSelectionID = -1; 
        tvtitle.setBackgroundResource(R.drawable.btn_bg); 
        pitchid = "0"; 

       } else { 
        stPitchSelectionID = v.getId(); 
        tvtitle.setBackgroundResource(R.drawable.btn_bg_select); 

        pitchid = String.valueOf(stPitchSelectionID) 
          .substring(1); 

       } 
      } 
     }); 
    } 
} 

而且來自Setdefaultvalue()我正在檢查我的活動回覆:

public void Setdefaultvalue() { 
    llpitchcontainer.removeAllViews(); 
    llbatresultcontainer.removeAllViews(); 
    llpitchresultcontainer.removeAllViews(); 
    llscoringcontainer.removeAllViews(); 
    etspeed.setText(65 + ""); 
    tvhh.setBackgroundResource(R.drawable.no_toggle); 

    String response_pitch=pref.getString(Preference_Data.PITCH_RESPONSE,""); 

    if (response_pitch.equalsIgnoreCase("")) { 
     mainScoutPresenter.pitch(); 
     Log.e("Pitch_if","Pitch_if"); 
    } else { 
      pitch(response_pitch); 
      Log.e("Pitch_else", "Pitch_else"); 
    } 
} 

這裏是我的兩個模型類:(PitchList_Res)

public class PitchList_Res implements Serializable { 


    @SerializedName("DATA") 
    @Expose 
    private List<PitchList_Data> dATA = null; 

    public List<PitchList_Data> getdATA() { 
     return dATA; 
    } 

    public void setdATA(List<PitchList_Data> dATA) { 
     this.dATA = dATA; 
    } 
} 

PitchList_Data.java

public class PitchList_Data implements Serializable { 

    @SerializedName("PITCHTYPEID") 
    @Expose 
    private Integer pITCHTYPEID; 
    @SerializedName("PITCHFULL") 
    @Expose 
    private String pITCHFULL; 
    @SerializedName("PITCHSHORT") 
    @Expose 
    private String pITCHSHORT; 

    public Integer getpITCHTYPEID() { 
     return pITCHTYPEID; 
    } 

    public void setpITCHTYPEID(Integer pITCHTYPEID) { 
     this.pITCHTYPEID = pITCHTYPEID; 
    } 

    public String getpITCHFULL() { 
     return pITCHFULL; 
    } 

    public void setpITCHFULL(String pITCHFULL) { 
     this.pITCHFULL = pITCHFULL; 
    } 

    public String getpITCHSHORT() { 
     return pITCHSHORT; 
    } 

    public void setpITCHSHORT(String pITCHSHORT) { 
     this.pITCHSHORT = pITCHSHORT; 
    } 
} 
+0

在這行你正在崩潰或異常喜歡給我的代碼塊來分析。 –

+0

當我調試我的APP我收到崩潰在我的改造呼叫。 Gson gson1 = new Gson(); String json = gson1.toJson(response); –

回答

0

我得到了同樣的問題&發現錯誤是由於 類型爲Object的類中的變量。

我在解析一個java類對象,它包含內部對象,其中一些對象包含Object類型的變量。由於哪個Gson庫無法解析這些數據。

我已經刪除了變量和問題得到解決的對象類型聲明。

我覺得GSON庫無法解析Java對象的JSON如果 java對象包含一些變量,泛型類型(如在我的情況下,對象 型)。

public class HatchRetailer { 
    private int id; 
    private String name; 
    private String purchaseLink; 
    private String purchaseLinkDirect; 
    private String logo; 
    private String currency; 
    private double price; 
    private String stockInfo; 
    private int stockCount; 
    private String sourceDate; 
    private String businessId; 

// As per my understanding Gson library is not able to parse variable of type Object 
// private Object marketFocus; 
// private Object group; 

// Setters & Getters 
} 
相關問題