How to get string response from Retrofit2?

前端 未结 10 1508
时光取名叫无心
时光取名叫无心 2020-12-01 07:44

I am doing android, looking for a way to do a super basic http GET/POST request. I keep getting an error:

java.lang.IllegalArgumentException: Unable to creat         


        
10条回答
  •  再見小時候
    2020-12-01 07:48

    You can try the following:

    build.gradle file:

    dependencies {
        ...
        compile 'com.squareup.retrofit2:retrofit:2.0.1'
        ...
    }
    

    WebAPIService:

    @GET("/api/values")
    Call getValues();
    

    Activity file:

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(API_URL_BASE)                    
                    .build();
    
            WebAPIService service = retrofit.create(WebAPIService.class);
    
            Call stringCall = service.getValues();
            stringCall.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    Log.i(LOG_TAG, response.body());
                }
    
                @Override
                public void onFailure(Call call, Throwable t) {
                    Log.e(LOG_TAG, t.getMessage());
                }
            });
    

    I have tested with my Web serivce (ASP.Net WebAPI):

    public class ValuesController : ApiController
    {
            public string Get()
            {
                return "value1";
            }
    }
    

    Android Logcat out: 04-11 15:17:05.316 23097-23097/com.example.multipartretrofit I/AsyncRetrofit2: value1

    Hope it helps!

提交回复
热议问题