2017-05-08 72 views
0

我正在學習改造,我只是試圖將json轉換爲gson。也許這是因爲我不熟悉鏈接的json格式的結構。我嘗試了更簡單的格式和應用程序的另一個鏈接。我想查詢「數據」中的內容。我希望你能給我一些建議,弄清楚什麼是錯的。順便說一下,我爲這兩個鏈接製作了單獨的應用程序。但是這個過程是相似的。應用程序崩潰而不顯示錯誤(Retrofit 2)

這裏是我應該是工作的鏈接:

Link that makes app crash

下面是本教程

working link

這裏給出的鏈接是我的接口:

public interface HTTPService { 

@GET("/organizations?type=json") 
Call<Organization> getMyJSON(); 

}

這裏是我的客戶:

public class HTTPClient { 
private static final String ROOT_URL = "http://isteward.tastradedev.com/api"; 

/** 
* Get Retrofit Instance 
*/ 
private static Retrofit getRetrofitInstance() { 
    return new Retrofit.Builder() 
      .baseUrl(ROOT_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
} 

/** 
* Get API Service 
* 
* @return API Service 
*/ 
public static HTTPService getApiService() { 
    return getRetrofitInstance().create(HTTPService.class); 
} 

}

這裏是我的MainActivvity:

public class MainActivity extends AppCompatActivity { 
private ListView listView; 
private View parentView; 

private ArrayList<Datum> datalist; 
private ListAdapter adapter; 
ProgressDialog dialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    datalist = new ArrayList<>(); 
    parentView = findViewById(R.id.parentlayout); 


    listView = (ListView) findViewById(R.id.listView); 


    dialog = new ProgressDialog(MainActivity.this); 
    dialog.setTitle("Fetching Data"); 
    dialog.setMessage("Please wait..."); 
    dialog.show(); 

    //Creating an object of our api interface 
    HTTPService api = HTTPClient.getApiService(); 

    Call<Organization> call = api.getMyJSON(); 

    call.enqueue(new Callback<Organization>() { 
     @Override 
     public void onResponse(Call<Organization> call, Response<Organization> response) { 
      //Dismiss Dialog 
      dialog.dismiss(); 

      if (response.isSuccessful()) { 

       datalist = response.body().getData(); 


       adapter = new ListAdapter(MainActivity.this, datalist); 
       listView.setAdapter(adapter); 

      } else { 
       Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call<Organization> call, Throwable t) { 

     } 
    }); 
} 

}

我使用jsonschema2pojo生成的類。 鏈接由3個類組成。我想這會讓我的帖子太長。感謝您的幫助。

編輯:我想我必須包括我的適配器

這裏是我的適配器的某些部分:

public class ListAdapter extends ArrayAdapter<Datum> { 
ArrayList<Datum> datalist; 
Context context; 

private LayoutInflater inflater; 


public ListAdapter(Context context, ArrayList<Datum> objects) { 
    super(context, 0, objects); 
    this.context = context; 
    this.inflater = LayoutInflater.from(context); 
    datalist = objects; 
} 

@Override 
public Datum getItem(int position) { 
    return datalist.get(position); 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    final ViewHolder vh; 
    if (convertView == null) { 
     View view = inflater.inflate(R.layout.row, parent, false); 
     vh = ViewHolder.create((RelativeLayout) view); 
     view.setTag(vh); 
    } else { 
     vh = (ViewHolder) convertView.getTag(); 
    } 
+0

嘗試使用okhttp HttpLoggingInterceptor從服務器調試您的響應。 –

+0

Android監視器中必須有日誌貓條目。請粘貼在這裏,我們需要檢查它。 – Vucko

+0

這裏呼叫<組織>您從服務器獲得組織,但api顯示它是小孩。如果你從jsonschema2pojo生成類然後使用Example類。如致電

回答

0

首先,當你使用改裝請按照以下

http://isteward.tastradedev.com 

請只提供改造域之後,應該在這樣的界面去

@GET("api/organizations?type=json") 

,請不要在根URL後添加任何斜槓

http://isteward.tastradedev.com 

@GET("/api/organizations?type=json") 

而且我還看到了一些空字符串在你的迴應。請將它們轉換爲空或傳遞其他內容(這可能是您的問題)。

+0

這個伎倆!我提出了我不需要的值,然後我將baseURL設置爲「http://isteward.tastradedev.com」。謝謝! – Dubutski

0

我的名譽不允許我評論了你的問題,但你不能只將json轉換爲字符串,然後將字符串轉換爲gson?

+0

抱歉,先生,但我的任務是做「自動gson」 – Dubutski

0

對於獲取特定數組/字符串,您必須使用父對象來獲取DATA。您無法直接使用該POJO獲取嵌套數組。所以更改API類按如下:

首先請從服務器接收整個JSON API接口:

@GET("/organizations?type=json") 
Call<Example> getMyJSON(); 

這裏的例類包含JSON resonse的的getter/setter。

現在,當您申請API,使用該類回調順利拿到響應,

Call<Example> call = api.getMyJSON(); 

    call.enqueue(new Callback<Example>() { 
     @Override 
     public void onResponse(Call<Example> call, Response<Example> response) { 
      //Dismiss Dialog 
      dialog.dismiss(); 

      if (response.isSuccessful()) { 

     /*** HERE response.body contains whole JSON, you need to pass through whole chain to get desire Data. SO Organization class conatains DATA so use it like***/ 

       datalist = response.body().getOrganization().getData();; 

     /--- datalist is list of Datum array list. now pass that to adapter ---/ 

       adapter = new ListAdapter(MainActivity.this, datalist); 
       listView.setAdapter(adapter); 

      } else { 
       Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show(); 
      } 
     } 

     @Override 
     public void onFailure(Call<Example> call, Throwable t) { 

     } 
    }); 
+0

除了代碼示例,請添加說明,以便有相似問題的人可以通過閱讀您的提示來獲取上下文。 – tommus

+0

仍然崩潰。當我追溯代碼的流程時,它應該起作用。我真的不知道是什麼讓應用程序崩潰。 – Dubutski

+0

然後發佈完整的錯誤日誌來解決它 –

相關問題