鍍金池/ 問答/Android/ 請(qǐng)教一個(gè)使用Retrofit做Get請(qǐng)求的問題,為什么獲取到的不是JSON數(shù)據(jù),

請(qǐng)教一個(gè)使用Retrofit做Get請(qǐng)求的問題,為什么獲取到的不是JSON數(shù)據(jù),而只是一個(gè)對(duì)象地址呢?

1.單例化Retrofit:

public class NetWork {
    private static Retrofit retrofit;
    /**返回Retrofit*/
    public static Retrofit getRetrofit(){
        if(retrofit==null){
            Retrofit.Builder builder = new Retrofit.Builder();//創(chuàng)建Retrfit構(gòu)建器
//            retrofit = builder.baseUrl("http://apis.juhe.cn/") //指定網(wǎng)絡(luò)請(qǐng)求的baseUrl
            retrofit = builder.baseUrl("http://v.juhe.cn/") //指定網(wǎng)絡(luò)請(qǐng)求的baseUrl
                    .addConverterFactory(GsonConverterFactory.create())//返回的數(shù)據(jù)通過Gson解析
                    .build();
        }
        return retrofit;
    }
}

2.封裝接口:

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

3.請(qǐng)求數(shù)據(jù):

  //初始化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) {
                                //請(qǐng)求成功
                                Bean bean = response.body();
                                Log.d(TAG, "1547=   "+bean); //這里打印的是  com.b.demo8.Bean@6a156c6
                            }

                            @Override
                            public void onFailure(Call<Bean> call, Throwable t) {
                                //請(qǐng)求失敗
                            }
                        });
回答
編輯回答
歆久
Java基礎(chǔ)知識(shí)了解一下

請(qǐng)求Url:http://v.juhe.cn/toutiao/inde...
返回?cái)?shù)據(jù):JSON

百度/Google:Java toString()

加號(hào)運(yùn)算會(huì)調(diào)用到對(duì)象的String()方法,如果你沒重寫這個(gè)方法,那就會(huì)調(diào)用Object的:

public class Object {
    public String toString() {
        return getClass().getName() + "@" + Integer.toHexString(hashCode());
    }
}
2017年6月16日 04:15
編輯回答
青裙

addConverterFactory(GsonConverterFactory.create())你已經(jīng)把json轉(zhuǎn)化成對(duì)象了

2018年3月28日 05:00
編輯回答
擱淺

請(qǐng)去掉所有 retrofit 代碼,執(zhí)行 :

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

看看結(jié)果。

2018年5月17日 02:43