Ask a question about using Retrofit for Get requests: why do you get an object address instead of JSON data?

1. Singleton Retrofit:

public class NetWork {
    private static Retrofit retrofit;
    /**Retrofit*/
    public static Retrofit getRetrofit(){
        if(retrofit==null){
            Retrofit.Builder builder = new Retrofit.Builder();//Retrfit
//            retrofit = builder.baseUrl("http://apis.juhe.cn/") //baseUrl
            retrofit = builder.baseUrl("http://v.juhe.cn/") //baseUrl
                    .addConverterFactory(GsonConverterFactory.create())//Gson
                    .build();
        }
        return retrofit;
    }
}

2. Encapsulation interface:

public interface NetInterface {
//    @GET("mobile/get")
    @GET("toutiao/index")
    Call<Bean> getAddress(@Query("type") String type, @Query("key") String key);
}

3. Request data:

  //Retrofit,
                NetInterface netInterface = NetWork.getRetrofit().create(NetInterface.class);
                netInterface.getAddress("15712101108","c727cca6fb450ea08b4b0fa220b72eb1")
                        .enqueue(new Callback<Bean>() {
                            @Override
                            public void onResponse(Call<Bean> call, Response<Bean> response) {
                                //
                                Bean bean = response.body();
                                Log.d(TAG, "1547=   "+bean); //  com.b.demo8.Bean@6a156c6
                            }

                            @Override
                            public void onFailure(Call<Bean> call, Throwable t) {
                                //
                            }
                        });
Mar.04,2021

learn the basics of Java

request Url: http://v.juhe.cn/toutiao/inde.
returned data: JSON

Baidu / Google: Java toString ()

The

plus sign operation calls the String () method of the object. If you do not override this method, it will call Object :

public class Object {
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
}

addConverterFactory (GsonConverterFactory.create ()) you have converted json into an object


Please remove all retrofit code and execute:

Bean bean = new Bean();
Log.d(TAG, "1547=   "+bean);

look at the results.

Menu