2017-07-26 19 views
0

Retrofit2のRESTful Webサービスで次のサービスエンドポイントを呼び出すにはどうすればよいですか?RetrofitでこのWebサービスを呼び出す方法

私のサービスエンドポイント:

http://crb.cloud/mileage/api/?expanse_view_user&Appkey=MiLeAgE&Userid=1&Vehid=1

私のコードは:

interface APIInterface { 
@GET("expanse_view_user/{Appkey}") 
Call<Example> getUserById(@Path("Appkey") String mlg, @Query("Userid") 
    Integer id , @Query("Vehid") Integer vehid); 

} 


class APIClient { 
private static Retrofit retrofit = null; 
static Retrofit getClient() { 
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor(); 
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY); 
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build(); 

    retrofit = new Retrofit.Builder() 
      .baseUrl("http://crb.cloud/mileage/api/?") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .client(client) 
      .build(); 
    //expanse_view_user&Appkey=MiLeAgE&Userid=1&Vehid=17 
    return retrofit; 
} 
} 


public class MainActivity extends AppCompatActivity { 

APIInterface apiInterface; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    apiInterface = APIClient.getClient().create(APIInterface.class); 
    Call<Example> call = apiInterface.getUserById("MiLeAgE",1,17); 
    call.enqueue(new Callback<Example>() { 
     @Override 
     public void onResponse(Call<Example> call, Response<Example> response) { 
      Log.e("TAG",response.code()+""); 

      String displayResponse = ""; 

      Example resource = response.body(); 
      Integer text = resource.status; 
      String total = resource.message; 
      } 
     } 
     @Override 
     public void onFailure(Call<Example> call, Throwable t) { 
     } 
    }); 
} 
} 
+0

投稿と書式の改善 – user1438038

答えて

0

あなたはHashMapを使用することができます。例えば:あなたはこの

@GET("?expanse_view_user") 
Call<Example> getData(@Query("Appkey") String Appkey, @Query("Userid") String Userid, @Query("Vehid") String Vehid); 

とベースURLのようなあなたのAPIを作成することができ

HashMap<String, String> params = new HashMap<>(); 
params.put("key1", "value1"); 
params.put("key2", "value2"); 
params.put("key3", "value3"); 

apiInterface = APIClient.getClient().create(APIInterface.class); 
Call<Example> call = apiInterface.getUserById(params); 
+0

応答コード404表示 –

+0

同じエラーが表示されます –

0

@GET("?expanse_view_user") 
Call<YourModelClass> getUserById(@QueryMap HashMap<String, String> params); 

サーバーに要求しているところから、あなたは次のように使用することができますas http://crb.cloud/mileage/api/

関連する問題