How to get string response from Retrofit2?

前端 未结 10 1478
时光取名叫无心
时光取名叫无心 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 08:04

    I change the Response type, instead of adding ScalarsConverterFactory like this

    interface YourApiService {
    @GET("/endpoint")
        fun getResponse(): Call<ResponseBody>
    }
    

    And then you can get the string object by

    val str = response.body().toString()

    0 讨论(0)
  • 2020-12-01 08:04
    String body = new String(((TypedByteArray) response.getBody()).getBytes());
    

    if you found

    java.lang.ClassCastException: retrofit.client.UrlConnectionClient$TypedInputStream cannot be cast to retrofit.mime.TypedByteArray
    

    then put this in your RestAdapter

    .setLogLevel(RestAdapter.LogLevel.FULL)
    

    for Example:

    RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setEndpoint(Root_url)
                .build();
    
    0 讨论(0)
  • 2020-12-01 08:05

    Add Retrofit2 add ScalarsConverterFactory to your Retrofit.Builder.

    adapterBuilder = new Retrofit.Builder()
                   .addConverterFactory(ScalarsConverterFactory.create())
                   .addConverterFactory(GsonConverterFactory.create());
    

    To use ScalarsCoverter add following dependency to your build graddle

    compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0' //Adding Retrofit2
    

    For API Call use: ``

    Call <String> *****
    

    Android Code :

    .enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Log.i("Response", response.body().toString());
            //Toast.makeText()
            if (response.isSuccessful()){
                if (response.body() != null){
                    Log.i("onSuccess", response.body().toString());
                }else{
                    Log.i("onEmptyResponse", "Returned empty response");//Toast.makeText(getContext(),"Nothing returned",Toast.LENGTH_LONG).show();
                }
            }
    
    0 讨论(0)
  • 2020-12-01 08:08

    I take a look at Retrofit library and noticed that it parses response according to the type class inside Call<T>. So you have two option: 1st: create a class according to the response from the server.

    2nd: get the response and handle it yourself (Not recommended Retrofit already handles it. So why do you use Retrofit as it is tailored for this job). Anyway instead of Call<String> use Call<ResponseBody> and Call<ResponseBody> signin = service.jquery(); after this put the following

    call.enqueue(new Callback<ResponseBody>() {
      @Override
      public void onResponse(Response<ResponseBody> response, Retrofit retrofit) {
        // handle success
       String result = response.body().string();
    
      }
    
      @Override
      public void onFailure(Throwable t) {
        // handle failure
      }
    });
    
    0 讨论(0)
提交回复
热议问题