如何在类之间发送一个字符串?
how to send a string between classes?
大家好,我想在 android 工作室的 java class 之间发送字符串。
我有 class CreateToken.java 和 MainActivity.java,如何发送 String yourToken
到 MainActivity.java 以及如何接收string yourToken
in MainActivity.java, yourToken 的结果是 com.example.user.application.CreateToken@yourToken
但 yourToken
不是完整的 token ,它只有 7 个字符。
这是我在 CreateToken.java 中的功能之一 :
public class CreateToken {
private ICreateToken listener;
public CreateToken(ICreateToken listener) {
this.listener = listener;
}
public Call<Token> api(final Context ctx){
ApiInterface api = ApiClient.getClient().create(ApiInterface.class);
String usernameApi = "web";
String passwordApi = "123";
Call<Token> getToken = api.postWebService(usernameApi,passwordApi);
getToken.enqueue(new Callback<Token>() {
@Override
public void onResponse(Call<Token> call, Response<Token> response) {
String error = response.body().getError();
if (error.equals("false")){
Toast.makeText(ctx, response.body().getToken(),Toast.LENGTH_SHORT).show();
Log.d("Smart","Response : Token Show");
String yourToken = response.body().getToken();
listener.onTokenGenerated(yourToken);
}else {
Toast.makeText(ctx, response.body().getMessage(),Toast.LENGTH_SHORT).show();
Log.d("Smart","Response : Token NUll");
}
}
@Override
public void onFailure(Call<Token> call, Throwable t) {
Log.d("Smart","Response : Token Null");
}
});
return getToken;
}
public interface ICreateToken {
void onTokenGenerated(String token);
}
}
这是我的 MainActivity.java :
public class MainActivity extends AppCompatActivity implements CreateToken.ICreateToken {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
CreateToken token = new CreateToken(MainActivity.this);
textView.setText(token.toString());
}
@Override
public void onTokenGenerated(String token) {
}
}
我认为您可以使用 Android 框架中的 AsyncTask class:
https://developer.android.com/reference/android/os/AsyncTask
然后使用方法 doInBackground 调用网络服务,onPostExecute 使用该调用的响应:
public ActivityExample extends AsyncTask <clazz1,clazz2,clazz3> {
doInBackGround(clazz1 clazz){
return result;
}
onPostExecute(clazz2 result){
}
}
在 CreateToken class 中创建接口 ICreateToken,如下所示
public interface ICreateToken {
void onTokenGenerated(String token);
}
同时在CreateToken中声明Interface字段class
private ICreateToken listener;
然后从您的 MainActivity 传递 CreateToken 中的上下文 class 像这样
CreateToken token = new CreateToken(MainActivity.this);
然后在 CreateToken 构造函数中初始化监听器
public CreateToken(ICreateToken listener) {
this.listener = listner;
}
最后你可以从 onResponse return 令牌通过
listener.onTokenGenerated(yourToken)
最后也是最重要的
MainActivity extends AppCompatActivity implements ICreateToken
在 MainActivity 中实现 ICreateToken,这将要求在 MainActivity 中实现 onTokenGenerated,您将收到您的令牌。
大家好,我想在 android 工作室的 java class 之间发送字符串。
我有 class CreateToken.java 和 MainActivity.java,如何发送 String yourToken
到 MainActivity.java 以及如何接收string yourToken
in MainActivity.java, yourToken 的结果是 com.example.user.application.CreateToken@yourToken
但 yourToken
不是完整的 token ,它只有 7 个字符。
这是我在 CreateToken.java 中的功能之一 :
public class CreateToken {
private ICreateToken listener;
public CreateToken(ICreateToken listener) {
this.listener = listener;
}
public Call<Token> api(final Context ctx){
ApiInterface api = ApiClient.getClient().create(ApiInterface.class);
String usernameApi = "web";
String passwordApi = "123";
Call<Token> getToken = api.postWebService(usernameApi,passwordApi);
getToken.enqueue(new Callback<Token>() {
@Override
public void onResponse(Call<Token> call, Response<Token> response) {
String error = response.body().getError();
if (error.equals("false")){
Toast.makeText(ctx, response.body().getToken(),Toast.LENGTH_SHORT).show();
Log.d("Smart","Response : Token Show");
String yourToken = response.body().getToken();
listener.onTokenGenerated(yourToken);
}else {
Toast.makeText(ctx, response.body().getMessage(),Toast.LENGTH_SHORT).show();
Log.d("Smart","Response : Token NUll");
}
}
@Override
public void onFailure(Call<Token> call, Throwable t) {
Log.d("Smart","Response : Token Null");
}
});
return getToken;
}
public interface ICreateToken {
void onTokenGenerated(String token);
}
}
这是我的 MainActivity.java :
public class MainActivity extends AppCompatActivity implements CreateToken.ICreateToken {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
CreateToken token = new CreateToken(MainActivity.this);
textView.setText(token.toString());
}
@Override
public void onTokenGenerated(String token) {
}
}
我认为您可以使用 Android 框架中的 AsyncTask class: https://developer.android.com/reference/android/os/AsyncTask
然后使用方法 doInBackground 调用网络服务,onPostExecute 使用该调用的响应:
public ActivityExample extends AsyncTask <clazz1,clazz2,clazz3> {
doInBackGround(clazz1 clazz){
return result;
}
onPostExecute(clazz2 result){
}
}
在 CreateToken class 中创建接口 ICreateToken,如下所示
public interface ICreateToken {
void onTokenGenerated(String token);
}
同时在CreateToken中声明Interface字段class
private ICreateToken listener;
然后从您的 MainActivity 传递 CreateToken 中的上下文 class 像这样
CreateToken token = new CreateToken(MainActivity.this);
然后在 CreateToken 构造函数中初始化监听器
public CreateToken(ICreateToken listener) {
this.listener = listner;
}
最后你可以从 onResponse return 令牌通过
listener.onTokenGenerated(yourToken)
最后也是最重要的
MainActivity extends AppCompatActivity implements ICreateToken
在 MainActivity 中实现 ICreateToken,这将要求在 MainActivity 中实现 onTokenGenerated,您将收到您的令牌。