-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added FakeInterceptor sample project
- Loading branch information
Showing
41 changed files
with
966 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
apply plugin: 'com.android.application' | ||
|
||
android { | ||
compileSdkVersion 26 | ||
buildToolsVersion "26.0.2" | ||
defaultConfig { | ||
applicationId "com.hrupin.fakeinterceptorsample" | ||
minSdkVersion 15 | ||
targetSdkVersion 26 | ||
versionCode 1 | ||
versionName "1.0" | ||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | ||
} | ||
buildTypes { | ||
release { | ||
minifyEnabled false | ||
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | ||
} | ||
} | ||
} | ||
|
||
dependencies { | ||
implementation fileTree(dir: 'libs', include: ['*.jar']) | ||
implementation 'com.android.support:appcompat-v7:26.1.0' | ||
implementation 'com.android.support:design:26.1.0' | ||
|
||
implementation 'com.squareup.retrofit2:retrofit:2.2.0' | ||
implementation 'com.squareup.retrofit2:converter-gson:2.2.0' | ||
implementation 'com.squareup.okhttp3:okhttp:3.9.0' | ||
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1' | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Add project specific ProGuard rules here. | ||
# You can control the set of applied configuration files using the | ||
# proguardFiles setting in build.gradle. | ||
# | ||
# For more details, see | ||
# http://developer.android.com/guide/developing/tools/proguard.html | ||
|
||
# If your project uses WebView with JS, uncomment the following | ||
# and specify the fully qualified class name to the JavaScript interface | ||
# class: | ||
#-keepclassmembers class fqcn.of.javascript.interface.for.webview { | ||
# public *; | ||
#} | ||
|
||
# Uncomment this to preserve the line number information for | ||
# debugging stack traces. | ||
#-keepattributes SourceFile,LineNumberTable | ||
|
||
# If you keep the line number information, uncomment this to | ||
# hide the original source file name. | ||
#-renamesourcefileattribute SourceFile |
26 changes: 26 additions & 0 deletions
26
android/FakeInterceptorSample/app/src/main/AndroidManifest.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="com.hrupin.fakeinterceptorsample"> | ||
|
||
<uses-permission android:name="android.permission.INTERNET"/> | ||
|
||
<application | ||
android:allowBackup="true" | ||
android:icon="@mipmap/ic_launcher" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme" | ||
android:name=".App"> | ||
<activity | ||
android:name=".LoginActivity" | ||
android:label="@string/app_name"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
</application> | ||
|
||
</manifest> |
4 changes: 4 additions & 0 deletions
4
android/FakeInterceptorSample/app/src/main/assets/mockapi/login.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"id":"uceruc65tfvifbvuyfd67t", | ||
"name":"John" | ||
} |
21 changes: 21 additions & 0 deletions
21
android/FakeInterceptorSample/app/src/main/java/com/hrupin/fakeinterceptorsample/App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.hrupin.fakeinterceptorsample; | ||
|
||
import android.app.Application; | ||
|
||
/** | ||
* Created by Igor Khrupin www.hrupin.com on 11/11/17. | ||
*/ | ||
|
||
public class App extends Application { | ||
private static App INSTANCE; | ||
|
||
public static App getInstance() { | ||
return INSTANCE; | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
INSTANCE = this; | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
...keInterceptorSample/app/src/main/java/com/hrupin/fakeinterceptorsample/LoginActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package com.hrupin.fakeinterceptorsample; | ||
|
||
import android.os.Bundle; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.text.TextUtils; | ||
import android.view.KeyEvent; | ||
import android.view.View; | ||
import android.view.View.OnClickListener; | ||
import android.view.inputmethod.EditorInfo; | ||
import android.widget.Button; | ||
import android.widget.EditText; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.hrupin.fakeinterceptorsample.retrofit.RestClient; | ||
|
||
import java.util.HashMap; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
|
||
public class LoginActivity extends AppCompatActivity { | ||
|
||
private EditText mEmailView; | ||
private EditText mPasswordView; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_login); | ||
mEmailView = findViewById(R.id.email); | ||
|
||
mPasswordView = findViewById(R.id.password); | ||
mPasswordView.setOnEditorActionListener(new TextView.OnEditorActionListener() { | ||
@Override | ||
public boolean onEditorAction(TextView textView, int id, KeyEvent keyEvent) { | ||
if (id == EditorInfo.IME_ACTION_DONE || id == EditorInfo.IME_NULL) { | ||
attemptLogin(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
}); | ||
|
||
Button mEmailSignInButton = findViewById(R.id.email_sign_in_button); | ||
mEmailSignInButton.setOnClickListener(new OnClickListener() { | ||
@Override | ||
public void onClick(View view) { | ||
attemptLogin(); | ||
} | ||
}); | ||
} | ||
|
||
private void attemptLogin() { | ||
// Reset errors. | ||
mEmailView.setError(null); | ||
mPasswordView.setError(null); | ||
|
||
// Store values at the time of the login attempt. | ||
String email = mEmailView.getText().toString(); | ||
String password = mPasswordView.getText().toString(); | ||
|
||
boolean cancel = false; | ||
View focusView = null; | ||
|
||
// Check for a valid password, if the user entered one. | ||
if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) { | ||
mPasswordView.setError(getString(R.string.error_invalid_password)); | ||
focusView = mPasswordView; | ||
cancel = true; | ||
} | ||
|
||
// Check for a valid email address. | ||
if (TextUtils.isEmpty(email)) { | ||
mEmailView.setError(getString(R.string.error_field_required)); | ||
focusView = mEmailView; | ||
cancel = true; | ||
} else if (!isEmailValid(email)) { | ||
mEmailView.setError(getString(R.string.error_invalid_email)); | ||
focusView = mEmailView; | ||
cancel = true; | ||
} | ||
|
||
if (cancel) { | ||
focusView.requestFocus(); | ||
} else { | ||
HashMap<String, String> map = new HashMap<>(); | ||
map.put("email", email); | ||
map.put("password", password); | ||
RestClient.getApiInterface().login(map).enqueue(new Callback<User>() { | ||
@Override | ||
public void onResponse(Call<User> call, Response<User> response) { | ||
if(response.isSuccessful()){ | ||
User user = response.body(); | ||
Toast.makeText(LoginActivity.this, "Logged in successful. ID=" + user.id + ", Name=" + user.name, Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<User> call, Throwable t) { | ||
|
||
} | ||
}); | ||
} | ||
} | ||
|
||
private boolean isEmailValid(String email) { | ||
//TODO: Replace this with your own logic | ||
return email.contains("@"); | ||
} | ||
|
||
private boolean isPasswordValid(String password) { | ||
//TODO: Replace this with your own logic | ||
return password.length() > 4; | ||
} | ||
} | ||
|
10 changes: 10 additions & 0 deletions
10
android/FakeInterceptorSample/app/src/main/java/com/hrupin/fakeinterceptorsample/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.hrupin.fakeinterceptorsample; | ||
|
||
/** | ||
* Created by Igor Khrupin www.hrupin.com on 11/11/17. | ||
*/ | ||
|
||
public class User { | ||
public String id; | ||
public String name; | ||
} |
25 changes: 25 additions & 0 deletions
25
...eptorSample/app/src/main/java/com/hrupin/fakeinterceptorsample/retrofit/ApiInterface.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.hrupin.fakeinterceptorsample.retrofit; | ||
|
||
|
||
import com.hrupin.fakeinterceptorsample.User; | ||
|
||
import java.util.HashMap; | ||
|
||
import retrofit2.Call; | ||
import retrofit2.http.Body; | ||
import retrofit2.http.Headers; | ||
import retrofit2.http.POST; | ||
|
||
/** | ||
* ServerInterface | ||
*/ | ||
public interface ApiInterface { | ||
|
||
@Headers({ | ||
"Accept: application/json", | ||
"Content-Type: application/json" | ||
}) | ||
@POST("login") | ||
Call<User> login(@Body HashMap<String, String> body); | ||
|
||
} |
122 changes: 122 additions & 0 deletions
122
...orSample/app/src/main/java/com/hrupin/fakeinterceptorsample/retrofit/FakeInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.hrupin.fakeinterceptorsample.retrofit; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.URI; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.Protocol; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
import okio.Buffer; | ||
|
||
/** | ||
* | ||
*/ | ||
public class FakeInterceptor implements Interceptor { | ||
private static final String TAG = FakeInterceptor.class.getSimpleName(); | ||
private static final String FILE_EXTENSION = ".json"; | ||
private Context mContext; | ||
|
||
private String mContentType = "application/json"; | ||
|
||
public FakeInterceptor(Context context) { | ||
mContext = context; | ||
} | ||
|
||
/** | ||
* Set content type for header | ||
* | ||
* @param contentType Content type | ||
* @return FakeInterceptor | ||
*/ | ||
public FakeInterceptor setContentType(String contentType) { | ||
mContentType = contentType; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
List<String> listSuggestionFileName = new ArrayList<>(); | ||
String method = chain.request().method().toLowerCase(); | ||
|
||
Response response = null; | ||
// Get Request URI. | ||
final URI uri = chain.request().url().uri(); | ||
Log.d(TAG, "--> Request url: [" + method.toUpperCase() + "]" + uri.toString()); | ||
|
||
String defaultFileName = getFileName(chain); | ||
|
||
if (defaultFileName != null) { | ||
String fileName = getFilePath(uri, defaultFileName); | ||
Log.d(TAG, "Read data from file: " + fileName); | ||
try { | ||
InputStream is = mContext.getAssets().open(fileName); | ||
BufferedReader r = new BufferedReader(new InputStreamReader(is)); | ||
StringBuilder responseStringBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = r.readLine()) != null) { | ||
responseStringBuilder.append(line).append('\n'); | ||
} | ||
Log.d(TAG, "Response: " + responseStringBuilder.toString()); | ||
Response.Builder builder = new Response.Builder(); | ||
builder.request(chain.request()); | ||
builder.protocol(Protocol.HTTP_1_0); | ||
builder.addHeader("content-type", mContentType); | ||
|
||
builder.body(ResponseBody.create(MediaType.parse(mContentType), responseStringBuilder.toString().getBytes())); | ||
builder.code(200); | ||
builder.message(responseStringBuilder.toString()); | ||
|
||
response = builder.build(); | ||
} catch (IOException e) { | ||
Log.e(TAG, e.getMessage(), e); | ||
} | ||
} else { | ||
for (String file : listSuggestionFileName) { | ||
Log.e(TAG, "File not exist: " + getFilePath(uri, file)); | ||
} | ||
response = chain.proceed(chain.request()); | ||
} | ||
|
||
Log.d(TAG, "<-- END [" + method.toUpperCase() + "]" + uri.toString()); | ||
return response; | ||
} | ||
|
||
private String getFileName(Chain chain) { | ||
|
||
String method = chain.request().method(); | ||
if (method.equals("POST")) { | ||
String fileName = null; | ||
String lastSegment = chain.request().url().pathSegments().get(chain.request().url().pathSegments().size() - 1); | ||
|
||
if (lastSegment.equals("login")) { | ||
fileName = "login"; | ||
} else { | ||
fileName = lastSegment; | ||
} | ||
|
||
return fileName + FILE_EXTENSION; | ||
} | ||
return "none" + FILE_EXTENSION; | ||
} | ||
|
||
private String getFilePath(URI uri, String fileName) { | ||
String path; | ||
if (uri.getPath().lastIndexOf('/') != uri.getPath().length() - 1) { | ||
path = uri.getPath().substring(0, uri.getPath().lastIndexOf('/') + 1); | ||
} else { | ||
path = uri.getPath(); | ||
} | ||
return uri.getHost() + path + fileName; | ||
} | ||
} |
Oops, something went wrong.