如何使用改造库在Android中的JsonObject中获取单独的Id数据

时间:2018-02-11 18:37:54

标签: android json retrofit

我是Android平台的新手。厌倦了使用改造从这样的链接获取数据 我想使用该链接作为Indivisual ID数据,如1,2,3,... 我已经尝试了很多次,但它显示空...... !!! 没有数据

https://jsonplaceholder.typicode.com/users/1

  

我的Api interfatce Class就像

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

        Api api = retrofit.create(Api.class);

        Call<List<patientsJson>> call = api.getPatientsJson();
        call.enqueue(new Callback<List<patientsJson>>() {
            @Override
            public void onResponse(Call<List<patientsJson>> call, Response<List<patientsJson>> response) {
                List<patientsJson> patientsJsons = response.body();

                for (patientsJson data: patientsJsons ){

                    t1.setText(data.getName());
                    t2.setText(data.getId());
                    t3.setText(data.getEmail());

                    Log.d("Name: ", data.getName() );
                    //Log.d("ID: ", data.getId());
                    Log.d("Email: ", data.getEmail());
                }
            }

            @Override
            public void onFailure(Call<List<patientsJson>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_LONG).show();

            }
        });

每次都会更改id值,并且来自另一个Activity类的不同按钮。

  

我的病人_活动类

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

    Button b = (Button)findViewById(R.id.button);
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this, patients_Activity.class);
            //intent.putExtra("ID", "1");
            startActivity(intent);
        }
    });
}
  

我的主要活动类

  //private String postId;
    private String id;
    private String name;
    private String email;

    public patientsJson( String id, String name, String email) {
        this.id = id;
        this.name = name;
        this.email = email;
    }


    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getEmail() {
        return email;
    }
  

我的病人Json班是

output_list = []
items = []
with open('spark.txt') as spark:
    for line in spark:
        line = line.rstrip()
        if line and not line.startswith('%'):
            if 'ALL Banks Report' in line:
                if items:
                    output_list.extend(items)
                items = [line]
            else:
                items.append(line)
if items:
    output_list.extend(items)

for item in output_list:
    print (item)

1 个答案:

答案 0 :(得分:1)

您可以从代码中动态传递id。您只需要为

编写改进界面
@GET("{id}")
Call<List<patientsJson>> getPatientsJson(@Path("id") int id);

现在,在调用Web服务时,您需要将ID作为参数传递。