2017-11-08 20 views
2

ハイIamは、新たな改造ライブラリを使用してデータを取得し、することができない私のJSONデータここ改造や改造ライブラリを使用してURLからデータを取得しようと目指して

enter image description here

私は名前を表示したいと思いますすべての配列。

、ここではgsonを変換POJOクラスです

RestResponse.java

public class RestResponse { 
    @SerializedName("messages") 
    @Expose 
    private List<String> messages = null; 
    @SerializedName("result") 
    @Expose 
    private List<Result> result = null; 

    public List<String> getMessages() { 
     return messages; 
    } 

    public void setMessages(List<String> messages) { 
     this.messages = messages; 
    } 

    public List<Result> getResult() { 
     return result; 
    } 

    public void setResult(List<Result> result) { 
     this.result = result; 
    } 
} 

2つ目Result.java

public class Result { 
    @SerializedName("name") 
    @Expose 
    private String name; 
    @SerializedName("alpha2_code") 
    @Expose 
    private String alpha2Code; 
    @SerializedName("alpha3_code") 
    @Expose 
    private String alpha3Code; 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getAlpha2Code() { 
     return alpha2Code; 
    } 

    public void setAlpha2Code(String alpha2Code) { 
     this.alpha2Code = alpha2Code; 
    } 

    public String getAlpha3Code() { 
     return alpha3Code; 
    } 

    public void setAlpha3Code(String alpha3Code) { 
     this.alpha3Code = alpha3Code; 
    } 

} 

し、最終的にRestResponseMain.java // Example.java Gson Converterによって生成されたファイル

public class RestResponseMain { 

    @SerializedName("RestResponse") 
    @Expose 
    private RestResponse restResponse; 

    public RestResponse getRestResponse() { 
     return restResponse; 
    } 

    public void setRestResponse(RestResponse restResponse) { 
     this.restResponse = restResponse; 
    } 
} 

と私のApiclenitファイルは次のとおりです:私は

実際のURLでデータを取得しようとしていた上記のクラスを使用することにより

がある

public class ApiClient { 
    public static final String BaseUrl="http://services.groupkt.com"; 
    public static Retrofit retrofit = null; 
    public static Retrofit getData() 
    { 
     if(retrofit==null) 
     { 
      retrofit = new Retrofit.Builder().baseUrl(BaseUrl).addConverterFactory(GsonConverterFactory.create()).build(); 
     } 



     return retrofit; 
    } 
} 

とApiInterfaceは

public interface ApiInterface { 
    @GET("/country/get/all") 
    Call<RestResponse> getData(); 
} 
です

とMainActivityで使用しているアクションは、

とアダプタファイルで、私は最終的に私はコンセプトを改造して新しいといくつかのオンラインチュートリアルでこれをしようとIAMプロセスとIAM上から任意の応答を得るdidntは

holder.tvName.setText(data.get(position).getName());//using recycler & card view 

を使用していた。

上記のコードでpojoクラスごとに考えられる変更を提案してください。この例 */

private static ServiceInterface apiService; 

public ServiceInterface callWSAds() { 

    if (!isInternetAvailable()) { 

    } 

    OkHttpClient.Builder builder = new OkHttpClient().newBuilder(); 

    builder.readTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS) 
      .writeTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS) 
      .connectTimeout(HTTP_TIMEOUT, TimeUnit.SECONDS) 
      .addInterceptor(new Interceptor() { 
       @Override 
       public okhttp3.Response intercept(Chain chain) throws IOException { 

        Request originalRequest = chain.request(); 
        Request.Builder requestBuilder = originalRequest.newBuilder().method(originalRequest.method(), originalRequest.body()); 

        Request request = requestBuilder.build(); 
        return chain.proceed(request); 

       } 
      }); 

    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    builder.addInterceptor(interceptor); 

    OkHttpClient client = builder.build(); 
    Retrofit retrofit; 
    retrofit = new Retrofit.Builder() 
      .baseUrl(DEV_BASE_URL) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .client(client) 
      .build(); 

    apiService = retrofit.create(ServiceInterface.class); 

    return apiService; 
} 

long HTTP_TIMEOUT = 80; 

callWSAds().getAdsSettingsNew(map).enqueue(new Callback<AdsSetting>() { //Need to change here 
      @Override 
      public void onResponse(Call<AdsSetting> call, Response<AdsSetting> response) { 
      if (response.body() != null) { 
       saveResponseOffline(response.body()); 
       setting = getResponseOffline(); 

       launchApp();//To track the launches of application 
      } 
     } 

     @Override 
     public void onFailure(Call<AdsSetting> call, Throwable t) { 
      if (setting == null) { 
       loadJSONFromAsset(); 
      } 
     } 
    }); 

をCALLING

答えて

1

RestResponseMain

にあなただけ変更する必要があるので、あなたは間違ったモデルのcalssを結合しても、あなたのベースURLが"/"

RestResponse

で終了する必要があります

AP Iクライアント

APIインタフェースとMainActivty.show

以下のAPIインタフェースで
public class ApiClient { 
    public final String baseUrl="http://services.groupkt.com/"; 
    public static Retrofit retrofit; 

    public static Retrofit getData() { 

     if (retrofit == null) { 
      retrofit = new Retrofit.Builder() 
          .baseUrl(BaseUrl) 
          .addConverterFactory(GsonConverterFactory.create()) 
          .build(); 
     } 
     return retrofit; 
    } 
} 

public interface ApiInterface { 
@GET("country/get/all") 
Call<RestResponseMain> getData(); 
} 

MainActivity

ApiInterface apiInterface = ApiClient.getData().create(ApiInterface.class); 
      Call<RestResponseMain> call = apiInterface.getData(); 
      pDialog.show(); 
      call.enqueue(new Callback<RestResponseMain>() { 
       @Override 
       public void onResponse(Call<RestResponseMain> call, Response<RestResponseMain> response) { 
        pDialog.dismiss(); 
        List<Result> list = response.body().getResult(); 
        MyRecyclerRetrofitAdapter adapter = new MyRecyclerRetrofitAdapter(MainActivity.this,list); 
        rView.setAdapter(adapter); 
       } 

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

       } 
      }); 
+0

おかげで仲間リストリスト= response.body()getRestResponse()のgetResult();。。これによってあなたの提案に沿って解決された問題 –

+0

@durgaprasadkalapatiあなたを助けることができます – Munir

1

/** * APIは、POSTメソッドの呼び出しが含まれています。 APIの呼び出しをより簡単にするには、この種の設定を使用してください。

1

ベースURLが指定されている "/" で終わる必要がありますRetrofitのドキュメント。

APIClient:

public class ApiClient { 
    public final String baseUrl="http://services.groupkt.com/"; 
    public static Retrofit retrofit; 

    public static Retrofit getData() { 

     if (retrofit == null) { 
      retrofit = new Retrofit.Builder() 
          .baseUrl(BaseUrl) 
          .addConverterFactory(GsonConverterFactory.create()) 
          .build(); 
     } 
     return retrofit; 
    } 
} 

APIInterface:

public interface ApiInterface { 
    @GET("country/get/all") 
    Call<RestResponse> getData(); 
} 
関連する問題