Reputation: 1509
I'm new to android, coming from iOS I don't know much about Java and all of its features. I'm trying to build an application where the user need to log at launch. I'm using a private API which I use like :
https://apiUrl.com/login?login=login&password=password
It returns me a JSon Object :
{
token: "qqdpo9i7qo3m8lldksin6cq714"
}
So what I'm doing in my code is simple :
MainActivity.java :
Button button = (Button) findViewById(R.id.loginButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String login = (String) ((EditText) findViewById (R.id.userName)).getText().toString();
String password = (String) ((EditText) findViewById (R.id.password)).getText().toString();
if (login != "" && password != "")
{
HashMap<String, String> postElements = new HashMap<String, String>();
postElements.put("login", login);
try {
postElements.put("password", URLEncoder.encode(password, "utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Button button = (Button) findViewById(R.id.loginButton);
button.setText("Login in ...");
String queryLogin = "https://apiUrl.com/login?";
String urlString = "";
try {
urlString = "login=";
urlString += URLEncoder.encode(login, "UTF-8");
urlString += "&password=";
urlString += URLEncoder.encode(password, "UTF-8");
} catch (UnsupportedEncodingException e) {
// if this fails for some reason, let the user know why
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
apiQuery.loginQuery(queryLogin, urlString);
}
apiQuery is of type APIQuery :
public void loginQuery(String url, String urlString) {
// Prepare your search string to be put in a URL
// It might have reserved characters or something
// Create a client to perform networking
AsyncHttpClient client = new AsyncHttpClient();
// Have the client get a JSONArray of data
// and define how to respond
client.get(url + urlString,
new JsonHttpResponseHandler() {
@Override
public void onSuccess(JSONObject jsonObject) {
String token = "";
if (jsonObject.has("token")) {
/*Toast.makeText(_mainContext, "Login Success!", Toast.LENGTH_LONG).show();*/
token = jsonObject.optString("token");
// 8. For now, just log results
Log.d("APIQuery Success", jsonObject.toString());
}
}
@Override
public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
// Display a "Toast" message
// to announce the failure
Toast.makeText(_mainContext, "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
// Log error message
// to help solve any problems
Log.e("APIQuery Failure", statusCode + " " + throwable.getMessage());
}
});
}
My implementation works fine, I have a ToastMessage which appears on the screen with "Login Success" (or "Login Error" when it fails of course)
But I don't know how to handle that success in order to pass to the other activity I created .
I would like to do something like this :
if (apiQuery.loginQuery(...))
show(activityLogged); // Where activityLogged is another activity
UPDATE
I added these lines :
if (jsonObject.has("token"))
{
/*Toast.makeText(_mainContext, "Login Success!", Toast.LENGTH_LONG).show();*/
token = jsonObject.optString("token");
// 8. For now, just log results
Log.d("APIQuery Success", jsonObject.toString());
Intent i = new Intent(_mainContext, MainActivityLogged.class);
_mainContext.startActivity(i);
}
And my manifest file looks like :
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!-- ATTENTION: This data URL was auto-generated. We recommend that you use the HTTP scheme.
TODO: Change the host or pathPrefix as necessary. -->
<data
android:host="epidroid.charvoz.example.com"
android:pathPrefix="/mainactivitylogged"
android:scheme="http" />
</intent-filters>
Upvotes: 1
Views: 1237
Reputation: 4586
You could simply write an Intent to move to the next activity inside your onSuccess callback
@Override
public void onSuccess(JSONObject jsonObject) {
String token = "";
if (jsonObject.has("token")) {
/*Toast.makeText(_mainContext, "Login Success!", Toast.LENGTH_LONG).show();*/
token = jsonObject.optString("token");
Intent i = new Intent(context,LoggedActivity.class);
context.startActivity(i);
}
}
In the above code
Intent i = new Intent(context,LoggedActivity.class);
startActivity(i);
This is used to navigate to next page.Also Make sure you declare the activity inside the manifest file.
Upvotes: 1
Reputation: 100
You could also pass some data to the next activity through the intent like so:
@Override
public void onSuccess(JSONObject jsonObject) {
String token = "";
if (jsonObject.has("token")) {
/*Toast.makeText(_mainContext, "Login Success!", Toast.LENGTH_LONG).show();*/
token = jsonObject.optString("token");
Intent i = new Intent(context,LoggedActivity.class);
i.putExtra("token", token);
startActivity(i);
}
}
And then retrieve the token from the next activity (inside the onCreate()) like so:
String token = getIntent().getStringExtra("token");
Upvotes: 0