mirror of
https://github.com/samuelclay/NewsBlur.git
synced 2025-09-18 21:50:56 +00:00
Added GSON library. Added method for handling responses from login request and displaying the relevant message.
This commit is contained in:
parent
0972b15c3c
commit
53795db6fe
8 changed files with 113 additions and 54 deletions
BIN
media/android/NewsBlur/libs/gson-2.1.jar
Normal file
BIN
media/android/NewsBlur/libs/gson-2.1.jar
Normal file
Binary file not shown.
|
@ -1,48 +1,66 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:id="@+id/login_viewswitcher"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="fill_parent" >
|
||||
|
||||
<EditText
|
||||
android:id="@+id/login_username"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/login_username_hint"
|
||||
android:inputType="textEmailAddress"
|
||||
android:textSize="22dp" />
|
||||
<RelativeLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="fill_parent" >
|
||||
|
||||
<EditText
|
||||
android:id="@+id/login_password"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/login_username"
|
||||
android:layout_marginTop="40dp"
|
||||
android:hint="@string/login_password_hint"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="textPassword"
|
||||
android:nextFocusDown="@+id/login_button"
|
||||
android:textSize="22dp" />
|
||||
<EditText
|
||||
android:id="@+id/login_username"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:hint="@string/login_username_hint"
|
||||
android:inputType="textEmailAddress"
|
||||
android:textSize="22dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/signup_button"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_marginTop="60dp"
|
||||
android:layout_marginBottom="40dp"
|
||||
android:contentDescription="@string/description_signup_button"
|
||||
android:text="@string/login_button_signup" />
|
||||
<EditText
|
||||
android:id="@+id/login_password"
|
||||
android:layout_width="fill_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/login_username"
|
||||
android:layout_marginTop="40dp"
|
||||
android:hint="@string/login_password_hint"
|
||||
android:imeOptions="actionDone"
|
||||
android:inputType="textPassword"
|
||||
android:nextFocusDown="@+id/login_button"
|
||||
android:textSize="22dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/login_button"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_marginTop="60dp"
|
||||
android:layout_marginBottom="40dp"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:contentDescription="@string/description_login_button"
|
||||
android:text="@string/login_button_login" />
|
||||
<Button
|
||||
android:id="@+id/signup_button"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_marginBottom="40dp"
|
||||
android:layout_marginTop="60dp"
|
||||
android:contentDescription="@string/description_signup_button"
|
||||
android:text="@string/login_button_signup" />
|
||||
|
||||
</RelativeLayout>
|
||||
<Button
|
||||
android:id="@+id/login_button"
|
||||
android:layout_width="120dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_marginBottom="40dp"
|
||||
android:layout_marginTop="60dp"
|
||||
android:contentDescription="@string/description_login_button"
|
||||
android:text="@string/login_button_login" />
|
||||
</RelativeLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:id="@+id/login_status"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content" >
|
||||
|
||||
<TextView
|
||||
android:id="@+id/login_status_text"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="logging in..." />
|
||||
</RelativeLayout>
|
||||
|
||||
</ViewSwitcher>
|
|
@ -2,8 +2,8 @@
|
|||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:gravity="center"
|
||||
android:background="@drawable/activity_background" >
|
||||
android:background="@drawable/activity_background"
|
||||
android:gravity="center" >
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/login_logo"
|
||||
|
@ -30,8 +30,8 @@
|
|||
<FrameLayout
|
||||
android:id="@+id/login_container"
|
||||
android:layout_width="280dp"
|
||||
android:layout_marginTop="50dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@+id/login_logo" />
|
||||
android:layout_below="@+id/login_logo"
|
||||
android:layout_marginTop="50dp" />
|
||||
|
||||
</RelativeLayout>
|
|
@ -7,6 +7,7 @@
|
|||
<string name="login_password_hint">password</string>
|
||||
<string name="login_button_login">Log In</string>
|
||||
<string name="login_button_signup">Sign Up</string>
|
||||
<string name="login_message_error">There was problem connecting to NewsBlur. Check your internet connection.</string>
|
||||
|
||||
<string name="hello">Hello World!</string>
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.newsblur.fragment;
|
||||
|
||||
import android.content.res.Resources;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.v4.app.Fragment;
|
||||
|
@ -9,14 +10,18 @@ import android.view.View.OnClickListener;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.Toast;
|
||||
import android.widget.ViewSwitcher;
|
||||
|
||||
import com.newsblur.R;
|
||||
import com.newsblur.network.APIManager;
|
||||
import com.newsblur.network.domain.LoginResponse;
|
||||
|
||||
public class LoginFragment extends Fragment implements OnClickListener {
|
||||
|
||||
public APIManager apiManager;
|
||||
private EditText username, password;
|
||||
private ViewSwitcher viewSwitcher;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
|
@ -25,6 +30,8 @@ public class LoginFragment extends Fragment implements OnClickListener {
|
|||
Button loginButton = (Button) v.findViewById(R.id.login_button);
|
||||
loginButton.setOnClickListener(this);
|
||||
|
||||
viewSwitcher = (ViewSwitcher) v.findViewById(R.id.login_viewswitcher);
|
||||
|
||||
username = (EditText) v.findViewById(R.id.login_username);
|
||||
password = (EditText) v.findViewById(R.id.login_password);
|
||||
|
||||
|
@ -45,25 +52,31 @@ public class LoginFragment extends Fragment implements OnClickListener {
|
|||
}
|
||||
}
|
||||
|
||||
private class LoginTask extends AsyncTask<String, Void, Boolean> {
|
||||
private class LoginTask extends AsyncTask<String, Void, LoginResponse> {
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
|
||||
viewSwitcher.showNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Boolean doInBackground(String... params) {
|
||||
protected LoginResponse doInBackground(String... params) {
|
||||
final String username = params[0];
|
||||
final String password = params[1];
|
||||
return apiManager.login(username, password);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(Boolean result) {
|
||||
if (result) {
|
||||
protected void onPostExecute(LoginResponse result) {
|
||||
if (result.authenticated) {
|
||||
((LoginFragmentInterface) getActivity()).loginSuccessful();
|
||||
} else {
|
||||
viewSwitcher.showPrevious();
|
||||
if (result.errors != null && result.errors.message.length > 0) {
|
||||
Toast.makeText(getActivity(), result.errors.message[0], Toast.LENGTH_LONG).show();
|
||||
} else {
|
||||
Toast.makeText(getActivity(), Resources.getSystem().getString(R.string.login_message_error), Toast.LENGTH_LONG).show();
|
||||
}
|
||||
((LoginFragmentInterface) getActivity()).loginUnsuccessful();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,9 @@ package com.newsblur.network;
|
|||
|
||||
import org.apache.http.HttpStatus;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.newsblur.network.domain.LoginResponse;
|
||||
|
||||
import android.content.ContentValues;
|
||||
import android.content.Context;
|
||||
|
||||
|
@ -13,16 +16,19 @@ public class APIManager {
|
|||
this.context = context;
|
||||
}
|
||||
|
||||
public boolean login(final String username, final String password) {
|
||||
public LoginResponse login(final String username, final String password) {
|
||||
APIClient client = new APIClient(context);
|
||||
final ContentValues values = new ContentValues();
|
||||
values.put(APIConstants.USERNAME, username);
|
||||
values.put(APIConstants.PASSWORD, password);
|
||||
final APIResponse response = client.post(APIConstants.URL_LOGIN, values);
|
||||
|
||||
// Consider the login complete if we've got a HTTP 200 and we've not been redirected
|
||||
// This could be made more granular depending on validity requirements.
|
||||
return (response.responseCode == HttpStatus.SC_OK && response.hasRedirected == false);
|
||||
if (response.responseCode == HttpStatus.SC_OK && response.hasRedirected == false) {
|
||||
Gson gson = new Gson();
|
||||
LoginResponse loginResponse = gson.fromJson(response.responseString, LoginResponse.class);
|
||||
return loginResponse;
|
||||
} else {
|
||||
return new LoginResponse();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package com.newsblur.network.domain;
|
||||
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
|
||||
public class LoginErrors {
|
||||
|
||||
@SerializedName("__all__")
|
||||
public String[] message;
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.newsblur.network.domain;
|
||||
|
||||
public class LoginResponse {
|
||||
|
||||
// {"code": -1, "authenticated": false, "errors": {"__all__": ["That username is not registered. Create an account with it instead."]}, "result": "ok"}
|
||||
|
||||
public boolean authenticated;
|
||||
public int code;
|
||||
public LoginErrors errors;
|
||||
public String result;
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue