从API Map / HashMap接收

时间:2016-12-07 03:48:21

标签: api dictionary hashmap retrofit

大家好我刚刚更新了我的代码,现在"所有内容"工作很好,我得到了连接,我收到了项目,我想将它添加到MAP,有人可以帮助我吗?

public class MainActivity extends AppCompatActivity {

    public static final String ENDPOINT_URL = "https://euw.api.pvp.net/";
    private TextView resultTv;
    private GetwilliamData getwilliamData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder().baseUrl(ENDPOINT_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        getwilliamData = retrofit.create(GetwilliamData.class);

        Button allbtn = (Button) findViewById(R.id.getallBtn);
        allbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadwilliam();
            }

        });


        Button onebtn = (Button) findViewById(R.id.oneBtn);
        onebtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadwilliam();

            }

        });

        resultTv = (TextView) findViewById(R.id.resultTV);
    }

    private void loadwilliam() {
        Call<SummonerData> call = getwilliamData.all();
        call.enqueue(new Callback<SummonerData>() {
            @Override
            public void onResponse(Call<SummonerData> call, Response<SummonerData> response) {
                System.out.println("HERE --------------------> " + call.request().url());
                SummonerData result = response.body();
                System.out.println("here ------------------------> " + response.body().getwilliam().getName());
                displayResult(result);
            }

            @Override
            public void onFailure(Call<SummonerData> call, Throwable t) {
                System.out.println("kdfsssssssssv -----------------> " + t.getMessage());
                System.out.println("HERE ---------------------> testing");
            }
        });
    }

    private void displayResult(SummonerData sd) { //throws java.lang.IllegalAccessException

        if (sd != null) {

            william r = sd.getwilliam();
            Map<String, Object> result = new HashMap<String, Object>();
            Field[] declaredFields = r.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
               // try {
                    //result.put(field.getName(), field.get(r));
                    result.put(field.getName(), field.getName());
                result.
             //   }
             //   catch (IllegalAccessException e){
                    result.put("Error", "reflection error");


             //   }
            }
            System.out.println(("--------------------------> id: " + result.get("id") ));
            resultTv.setText("id: " + result.get("id") );

        } else {
            resultTv.setText("Error asd");
        }

    }

}

这是问题

private void displayResult(SummonerData sd) { //throws java.lang.IllegalAccessException

if (sd != null) {

    william r = sd.getwilliam();
    Map<String, Object> result = new HashMap<String, Object>();
    Field[] declaredFields = r.getClass().getDeclaredFields();
    for (Field field : declaredFields) {
       // try {
            //result.put(field.getName(), field.get(r));
            result.put(field.getName(), field.getName());
        result.
     //   }
     //   catch (IllegalAccessException e){
            result.put("Error", "reflection error");


     //   }
    }
    System.out.println(("--------------------------> id: " + result.get("id") ));
    resultTv.setText("id: " + result.get("id") );

} else {
    resultTv.setText("Error asd");
}

我知道它没有工作/不良编码我正在进行一些测试..

额外:

SummonerData

public class SummonerData {
@SerializedName("william")
@Expose
    private william william;
/**
* 
* @return
* The william
*/
    public william getwilliam() {
    return william;
}
/**
* 
* @param william
* The william
*/
    public void setwilliam(william william) {
    this.william = william;
}

威廉

public class william {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("profileIconId")
@Expose
private Integer profileIconId;
@SerializedName("summonerLevel")
@Expose
private Integer summonerLevel;
@SerializedName("revisionDate")
@Expose
private long revisionDate;

/**
 * 
 * @return
 * The id
 */
public Integer getId() {
    return id;
}

/**
 * 
 * @param id
 * The id
 */
public void setId(Integer id) {
    this.id = id;
}

/**
 * 
 * @return
 * The name
 */
public String getName() {
    return name;
}

/**
 * 
 * @param name
 * The name
 */
public void setName(String name) {
    this.name = name;
}

/**
 * 
 * @return
 * The profileIconId
 */
public Integer getProfileIconId() {
    return profileIconId;
}

/**
 * 
 * @param profileIconId
 * The profileIconId
 */
public void setProfileIconId(Integer profileIconId) {
    this.profileIconId = profileIconId;
}

/**
 * 
 * @return
 * The summonerLevel
 */
public Integer getSummonerLevel() {
    return summonerLevel;
}

/**
 * 
 * @param summonerLevel
 * The summonerLevel
 */
public void setSummonerLevel(Integer summonerLevel) {
    this.summonerLevel = summonerLevel;
}

/**
 * 
 * @return
 * The revisionDate
 */
public long getRevisionDate() {
    return revisionDate;
}

/**
 * 
 * @param revisionDate
 * The revisionDate
 */
public void setRevisionDate(long revisionDate) {
    this.revisionDate = revisionDate;
}

GetWilliamData

public interface GetwilliamData {


@GET("api/lol/euw/v1.4/summoner/by-name/william?api_key=105bf501-8748-4e05-a913-ec570428375f")
Call<SummonerData> all();

@GET("api/lol/{server}/v1.4/summoner/by-name/{name}?api_key=105bf501-8748-4e05-a913-ec570428375f")
Call<william> select(@Path("name") String name);

}

感谢您的参与。

1 个答案:

答案 0 :(得分:0)

试试这个:

william r = sd.getwilliam();
    Map<String, Object> result = new HashMap<String, Object>();
    Field[] declaredFields = william.class.getDeclaredFields();
    for (Field field : declaredFields) {
        try {
            field.setAccessible(true);
            result.put(field.getName(), field.get(r));
        }
        catch (IllegalAccessException e){
            result.put("Error", "reflection error");


       }
    }