简体   繁体   中英

How can i pass a value to an interface in Java Android Studio from MainActivity to my Interface?

This is my MainActivity.java

    package com.stardash.stardash;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";
    private Button getData;

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

        getData = findViewById(R.id.getData);
        getData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                
                Methods methods = RetrofitClient.getRetrofitInstance().create(Methods.class);
                Call<Model> call = methods.getAllData();

                call.enqueue(new Callback<Model>() {
                    @Override
                    public void onResponse(Call<Model> call, Response<Model> response) {
                        Log.e(TAG, "onResponse code:"+response.code());

                        String data = response.body().getGender();

                        Log.e(TAG, data);

                        Button button = (Button) v;
                        button.setText(data);
                    }

                    @Override
                    public void onFailure(Call<Model> call, Throwable t) {
                        Log.e(TAG, "onFailure: ");

                    }
                });
            }
        });
    }
}

And this is my Interface

    package com.stardash.stardash;

import retrofit2.Call;
import retrofit2.http.GET;

public interface Methods {
    @GET("?name=joe")
    Call<Model> getAllData();
}

I tried using an Intent Methode but it doesnt seem to work with an interface. It only works with 2 Activities. So What i want to do, is to let the @GET in my interface take a name that i pass in my Mainactivity code. But first i need to figure out how to pass a String or value in the first place to an Interface.

public interface Methods {
    @GET
    Call<Model> getAllData(@Query("name") String name);
}

getAllData("smith") => baseUrl?name=smith

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM