how to send a string between classes? - java

hello guys I would like to sending string between java class in android studio.
I have class CreateToken.java and MainActivity.java, how can I send String yourToken to MainActivity.java and how can i receive string yourToken in MainActivity.java, and the result of yourToken is com.example.user.application.CreateToken#yourToken but yourToken is not full token , its just 7 charecter.
this is one of my function in 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);
}
}
and this is my 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) {
}
}

I think that you could use the AsyncTask class from Android framework:
https://developer.android.com/reference/android/os/AsyncTask
Then use the methods doInBackground to call the webservice and onPostExecute use the response from that call:
public ActivityExample extends AsyncTask <clazz1,clazz2,clazz3> {
doInBackGround(clazz1 clazz){
return result;
}
onPostExecute(clazz2 result){
}
}

create an interface ICreateToken within CreateToken class as below
public interface ICreateToken {
void onTokenGenerated(String token);
}
Also declare Interface field in CreateToken class
private ICreateToken listener;
and from your MainActivity pass context in CreateToken class like this
CreateToken token = new CreateToken(MainActivity.this);
then initialise the listener in CreateToken constructor
public CreateToken(ICreateToken listener) {
this.listener = listner;
}
finally from onResponse you can return token via
listener.onTokenGenerated(yourToken)
Last and most important
MainActivity extends AppCompatActivity implements ICreateToken
implement ICreateToken in MainActivity which will ask to implement onTokenGenerated in MainActivity there you'll receive your token.

Related

How to send 2 parameters in body with retrofit?

Good night everybody.
I need to do a POST request and I'm using Retrofit 2 to do this.
But the Api I'm consuming does not give me these parameters in the API response, just in the body.
Api Response
I already searched in some places but I did not find anything that could help me.
My interface Class
public interface LoginApi {
#POST("api/login")
Call<UserAccount> doLogin(#Body Login login);
}
My Model Class
public class Login {
public String user;
public String password;
}
My API response class
public class UserAccount {
#SerializedName("userId")
#Expose
private Integer userId;
#SerializedName("name")
#Expose
private String name;
#SerializedName("bankAccount")
#Expose
private String bankAccount;
#SerializedName("agency")
#Expose
private String agency;
#SerializedName("balance")
#Expose
private Double balance;
}
My call class
public class LoginPresenter {
private LoginView loginView;
private ServiceConfig serviceConfig;
public LoginPresenter() {
this.loginView = loginView;
if (this.serviceConfig == null) {
this.serviceConfig = new ServiceConfig();
}
}
public void doLogin(Login login) {
serviceConfig
.login()
.doLogin(login)
.enqueue(new Callback<UserAccount>() {
#Override
public void onResponse(Call<UserAccount> call, Response<UserAccount> response) {
UserAccount userAccount = response.body();
assert userAccount != null;
Log.e("Agency:",userAccount.getAgency());
Log.e("BankAccount:", userAccount.getBankAccount());
Log.e("Name:", userAccount.getName());
}
#Override
public void onFailure(Call<UserAccount> call, Throwable t) {
Log.d("Erro", t.getMessage());
}
});
}
}
My Activity
public class LoginActivity extends Activity implements LoginView {
private EditText edtUser, edtPassword;
private Button btnLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
init();
}
private void init() {
edtUser = findViewById(R.id.edt_user);
edtPassword = findViewById(R.id.edt_password);
btnLogin = findViewById(R.id.btn_login);
final LoginPresenter loginPresenter = new LoginPresenter();
final Login login = new Login();
login.user = edtUser.getText().toString();
login.password = edtPassword.getText().toString();
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loginPresenter.doLogin(login);
}
});
}
#Override
public void userAccount(List<UserAccount> userAccount) {
}
}
I hope I have made my problem clear and that someone who has been through it can help me.
Appreciate.
Pass #Body JsonObject body instead of #Body Login login
Here is full code:
Your interface will be:
public interface LoginApi {
#POST("api/login")
Call<UserAccount> doLogin(#Body JsonObject body);
}
How to Create JsonObject :
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("user", userValue);
jsonObject.addProperty("password", passwordValue);
Pass it from your activity to presenter.
Hope it will works for you.
Thank you.
Pass Like This:-
You Interface
public interface ApiInterface {
String URL_BASE = "Base Url";
#Headers("Content-Type: application/json")
#POST("login")
Call<User> getUser(#Body String body);
}
Activity
public class SampleActivity extends AppCompatActivity implements Callback<User> {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(ApiInterface.URL_BASE)
.addConverterFactory(ScalarsConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create())
.build();
ApiInterface apiInterface = retrofit.create(ApiInterface.class);
// prepare call in Retrofit 2.0
try {
JSONObject paramObject = new JSONObject();
paramObject.put("email", "sample#gmail.com");
paramObject.put("pass", "4384984938943");
Call<User> userCall = apiInterface.getUser(paramObject.toString());
userCall.enqueue(this);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onResponse(Call<User> call, Response<User> response) {
}
#Override
public void onFailure(Call<User> call, Throwable t) {
}
}

Sending a token between classes

I would like to sending string between java class in android studio. I have class CreateToken.java and MainActivity.java, how can I send string yourToken to MainActivity.java and how can I receive string yourToken in MainActivity.java, and the result of yourToken is com.example.user.application.CreateToken#yourToken but yourToken is not full token, its just 7 charecter.
This is one of my function in 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);
}
}
And this is my 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) {
}
}
You must call api like below for sending request to server:
CreateToken tokenCreator = new CreateToken(MainActivity.this);
tokenCreator.api(this);
and wait to triggered onTokenGenerated and using String token
#Override
public void onTokenGenerated(String token) {
textView.setText(token.toString());
}
You should have access to yourToken in MainActivity in
#Override
public void onTokenGenerated(String token) {
}
when listener.onTokenGenerated(yourToken); in CreateToken is executed. Just call public Call<Token> api(final Context ctx) method, and receive your token in MainActivity.

Instance variable in another class in Android

I seems having an issue where an instance variable is not updated in MainAtivity. the text still show "testing" instead of the message received by onMessage()
public class MainActivity extends AppCompatActivity {
pacioWebSocketListener myWS = new pacioWebSocketListener();
private String pacioMsg = myWS.getPacioMsg();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
TextView tv = findViewById(R.id.textView);
tv.setText("Message: " + pacioMsg);
And here is the pacioWebsocketListener.java
public class pacioWebSocketListener extends WebSocketListener {
private static final int NORMAL_CLOSURE_STATUS = 1000;
WebSocket ws;
public String pacioMsg = "testing";
public void setPacioMsg(String paciomsg){
pacioMsg = paciomsg;
}
public String getPacioMsg(){
return pacioMsg;
}
#Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Log.v(TAG,"onOpen");
}
#Override
public void onMessage(WebSocket webSocket, String message) {
super.onMessage(webSocket, message);
setPacioMsg(message);
In your activity, you are getting the value of pacioMsg before its value is changed by the method onMessage. onMessage is not called when you instantiate pacioWebSocketListener, it's only called in the event of a new message
So, If you want your activity to know every time a message arrives and update its textview, you need to find a way to make your activity and your class pacioWebSocketListener communicate.
You can do it by creating an Interface Listener like this:
in your class pacioWebSocketListener, create an interface
public class pacioWebSocketListener extends WebSocketListener {
MessageListener msgListener;
private static final int NORMAL_CLOSURE_STATUS = 1000;
WebSocket ws;
public pacioWebSocketListener(MessageListener listener){
this.msgListener = listener;
}
// interface
public interface MessageListener{
void onMessageReceived(String message)
}
#Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
Log.v(TAG,"onOpen");
}
#Override
public void onMessage(WebSocket webSocket, String message) {
super.onMessage(webSocket, message);
// When a new Message arrives , call the MessageListener.onMessageReceived
msgListener.onMessageReceived(message);
}
Now, in your activity implements the interface created in the pacioWebSocketListener class and override the method onMessageReceived, it goes like this
public class MainActivity extends AppCompatActivity implements pacioWebSocketListener.MessageListener {
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
// Pass "this" in the constructor, to show that this activity is
// Listening for changes
pacioWebSocketListener myWS = new pacioWebSocketListener(this);
tv = findViewById(R.id.textView);
}
// Override the method in MessageListener Inteface
public void onMessageReceived(String message){
// Then from here you can update your UI
tv.setText("Message: " + message);
}
Now Every time a new message arrives, your TextView will be updated with the text

Retrofit method response reusing into another activity

How can I get the data from getDataForId(Integer.toString(1)); by calling the same getDataForId method from the DisplayData class?
I want reuse the same method and get the result.
It doesn't make sense to copy and paste the same method into the other activity class. Then there will be the same code repeated twice.
This is my DisplayData.class
public class DisplayData extends AppCompatActivity {
Detail reqDetail;
String BASE_URL = "";
TextView name;
ImageView image;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_data);
name = (TextView) findViewById(R.id.name);
image = (ImageView)findViewById(R.id.image);
public void getDataForId(final String id) {
ApiInterface apiInterface = APIClient.getApiInterface();
Call<MyResponse> call = apiInterface.getResponse();
call.enqueue(new Callback<MyResponse>() {
#Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response.body() != null) {
MyResponse myResponse = response.body();
List<Detail> details = myResponse.getDetails();
for (Detail d : details) {
if (d.getId().equals(id)) {
reqDetail = d;
name.setText(reqDetail.getName());
Picasso.with(DisplayData.this)
.load(reqDetail.getName())
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(image);
}
}
}
}
#Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
}
This is my SecondData class where I want to display the same data response of DisplayData by reusing same methods
public class SecondData extends AppCompatActivity {
Detail reqDetail;
String BASE_URL = "";
TextView name;
ImageView image;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
name = (TextView) findViewById(R.id.name);
image = (ImageView)findViewById(R.id.image);
}
}
Create a class to make retrofit call like this
public class SampleClass {
private DataInterface mListener;
public SampleClass() {
super();
}
public void getDataForId(final String id) {
ApiInterface apiInterface = APIClient.getApiInterface();
Call<MyResponse> call = apiInterface.getResponse();
call.enqueue(new Callback<MyResponse>() {
#Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response!=null && response.body() != null && mListener != null) {
mListener.responseData(response.body());
}
}
#Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
}
public void setOnDataListener(DataInterface listener) {
mListener = listener;
}
public interface DataInterface {
void responseData( MyResponse myResponse );
}
}
And in your activity just call the class like this
SampleClass sampleClass = new SampleClass();
sampleClass.setOnDataListener(new SampleClass.DataInterface() {
#Override
public void responseData(MyResponse myResponse) {
}
});
sampleClass.getDataForId("UR ID");
Also in your class store the ID as private memeber variable
private Integer YOUR_ID;
Then on getting the result compare the result ID with this ID
List<Detail> details = myResponse.getDetails();
for (Detail d : details) {
if (d.getId().equals(YOUR_ID)) {
reqDetail = d;
name.setText(reqDetail.getName());
Picasso.with(DisplayData.this)
.load(reqDetail.getName())
.placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(image);
}
}
You should create a BaseActivity and extend the other two from it. Then, you place the method you want to use in both of them in the BaseActivity and make it public so you can use it properly.
Note that anything you need in your method from outside it won't be available in the BaseActivity, so you should either pass in the constructor or declare it in the BaseActivity if possible.
You should also think on how the return of your method should be for you to have access to its results.
You can refer to this question to learn more:
trying not to repeat myself (android/java)
So, If you want to use response of one Activity to another Activity,
Then, First save response to List or ArrayList.
And Then Pass data Using Intent.
That should do a trick.

Android: toast inside AsyncTask

I have an AsyncTask class SearchForQuestions that is called from an Activity QuizMap. When looping through an array in SearchForQuestions I can't find the correct context for toast to appear within the AsynTask.
The standard Toast.makeText(getApplicationContext(), "This is Toast!!!", Toast.LENGTH_SHORT).show(); gives error getApplicationContext() undefined.
I have tried some of the solutions to this offerred by SO, most of them are listed here and concern getting UiThread and running on that.
I can't get this to work however. Here's example code snippets of what i have tried. I have put a method in QuizMap and try calling it from SearchForQuestions but SearchForQuestions isn't recognised. How can I get around this? )Still a newbie at java...)
// QuizMap activity
public class QuizMap extends FragmentActivity
implements OnMarkerClickListener {
private GoogleMap map;
private static final String TAG = "QuizMap"; // debugging
...
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quizmap);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
...
}
// make toast inside AsyncTask
public void showNotNearToast(final String toast) {
QuizMap.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(QuizMap.this, "This is Toast!!!", Toast.LENGTH_SHORT).show();
}});
}
.
// SearchForQuestions class
private class SearchForQuestions extends AsyncTask<String, Void, DataHandler> {
// checks for proximity to question locations
Location location =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
#Override
protected DataHandler doInBackground(String... pointsList) {
String result = pointsList[0];
...
}
#Override
protected void onPostExecute(DataHandler result) {
ArrayList<String> resultsArray = result.results;
Integer numPoints = resultsArray.size();
for (int i =0;i<numPoints;i++){
String[] pointDetails = resultsArray.get(i).split("::");
...
// we can make use of the Android distanceTo function to calculate the distances
float distance = location.distanceTo(fixedLoc);
if (i > DIST) { // this is UCL
showNotNearToast("My Message"); // showNotNearToast undefined
if (distance < DIST) {
...
}
};
I'm going t close this question. I haven't solved my problem but the number of answers provided that apparently work in other situations suggest there's something else going on. I'm going to re-structure the classes to get around having to call from within AsyncTask.
Just Toast it, why do you want to create a function for it? onPostExecute() is already on UI thread.
You are not able to access because inner Class can not call functions of Outer class unless you pass instance of the outer class.
Call your toast in onPostExecute
Create an interface for a callback.
public interface ToastCallback {
public void invoke(String text);
}
Your AsyncTask constructor
private ToastCallback toastCallback;
public SearchQuestions(ToastCallback callback) {
this.toastCallback = callback;
}
// in doInBackground() {
toastCallback.invoke("Toast from background");
}
In Your Activity,
private void showNotNearToast(String text) {
runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
});
}
public class MyToastCallback implements ToastCallback {
#Override
public void invoke(String text) {
showNotNearToast(text);
}
}
// Asynctask call
new SearchQuestion(new MyTosatCallback()).execute(<Your params here>);
Try this from inside your AsyncTask:
myActivity.this.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Hello", Toast.LENGTH_SHORT).show();
}
});
Where you have your
showNotNearToast("My Message"); // showNotNearToast undefined
Replace myActivity with the name of your Activity.
(Ab)use the publishProgress method
private class ToastAsyncTask extends AsyncTask<Void, String, Void>{
#Override
protected Void doInBackground(Void... voids) {
SystemClock.sleep(1000);
publishProgress("Toast msg string");
SystemClock.sleep(1000);
return null;
}
#Override
protected void onProgressUpdate(String... values) {
Toast.makeText(getApplicationContext(), values[0], Toast.LENGTH_SHORT).show();
}
}
**UPDATE: ** since you are having problems with context for some reason, use this version. Tough the implementation above works for me.
private class ToastAsyncTask extends AsyncTask<Void, String, Void> {
private WeakReference<Context> contextRef;
public ToastAsyncTask(Context context) {
contextRef = new WeakReference<Context>(context);
}
#Override
protected Void doInBackground(Void... voids) {
SystemClock.sleep(1000);
publishProgress("Toast msg string");
SystemClock.sleep(1000);
return null;
}
#Override
protected void onProgressUpdate(String... values) {
if (contextRef.get() != null) {
Toast.makeText(contextRef.get(), values[0], Toast.LENGTH_SHORT).show();
} else {
// The context was destroyed.. check what you are doing
}
}
}
Use it like this
new ToastAsyncTask(MainActivity.this).execute();
Pass the activity into the AsyncTask. See below.
private class SearchForQuestions extends AsyncTask<String, Void, DataHandler> {
Activity activity;
public void SearchForQuestions(Activity activity){
this.activity = activity;
}
//... rest of the code
public class QuizMap extends FragmentActivity implements OnMarkerClickListener {
/*...*/
new SearchForQuestions(this).execute();
/*...*/
/*When calling the toast:*/
Toast.makeText(this.activity, "This is Toast!!!", Toast.LENGTH_SHORT).show();

Categories

Resources