Skip to content

Commit 01a3018

Browse files
committed
test highlights
1 parent 8f8ca83 commit 01a3018

File tree

7 files changed

+164
-4
lines changed

7 files changed

+164
-4
lines changed

android/mooc/androidapp/app/build.gradle

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,18 @@ dependencies {
4444
implementation fileTree(dir: 'libs', include: ['*.jar'])
4545
implementation 'com.android.support:appcompat-v7:26.1.0'
4646
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
47+
implementation 'com.android.support:support-v4:26.1.0'
4748
testImplementation 'junit:junit:4.12'
4849
androidTestImplementation 'com.android.support.test:runner:1.0.1'
4950
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
5051

52+
// https://developer.android.com/guide/topics/ui/layout/recyclerview.html#add-support-library
53+
implementation 'com.android.support:recyclerview-v7:26.1.0'
54+
5155
compile 'com.fasterxml.jackson.core:jackson-databind:2.5.3'
5256
compile 'com.loopj.android:android-async-http:1.4.9'
5357
// https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient
5458
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.5'
59+
// https://github.com/square/okhttp
60+
implementation 'com.squareup.okhttp3:okhttp:3.10.0'
5561
}

android/mooc/androidapp/app/src/main/java/io/stackdocker/mooc/andapp/MainActivity.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,24 @@
55
import android.os.Bundle;
66

77
import android.os.AsyncTask;
8+
import android.support.v7.widget.LinearLayoutManager;
9+
import android.support.v7.widget.RecyclerView;
810
import android.util.Log;
911
import android.view.View;
1012
import android.view.View.OnClickListener;
1113
import android.widget.Button;
1214
import android.widget.EditText;
1315

16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
import com.fasterxml.jackson.databind.type.MapType;
18+
import com.fasterxml.jackson.databind.type.TypeFactory;
1419
import com.loopj.android.http.AsyncHttpClient;
1520
import com.loopj.android.http.AsyncHttpResponseHandler;
1621

1722
import java.io.IOException;
1823
import java.io.InputStream;
24+
import java.nio.file.Paths;
25+
import java.util.HashMap;
1926

2027
import org.apache.http.HttpEntity;
2128
import org.apache.http.HttpResponse;
@@ -29,7 +36,18 @@
2936

3037
public class MainActivity extends AppCompatActivity {
3138
private static final String TAG = MainActivity.class.getSimpleName();
32-
private static final String API_PATH = "http://192.168.0.20:8080/apis";
39+
//private static final String API_PATH = "http://192.168.0.20:8080/apis";
40+
private final String API_HOST = getResources().getString(R.string.api_host);
41+
private final String APIS_PATH = API_HOST + "/apis";
42+
// private final String TOP_CLASS_PATH = Paths.get(API_HOST,
43+
// "api/v1alpha", getResources().getString(R.string.top_classes_path)).toString();
44+
private final String TOP_CLASS_PATH = API_HOST +
45+
"/api/v1alpha/namespaces/default/" + getResources().getString(R.string.top_classes_path);
46+
47+
private RecyclerView mRecyclerView;
48+
private RecyclerView.Adapter mAdapter;
49+
private RecyclerView.LayoutManager mLayoutManager;
50+
private HashMap<String, Object> myDataset;
3351

3452
private AsyncHttpClient client;
3553

@@ -38,8 +56,23 @@ protected void onCreate(Bundle savedInstanceState) {
3856
super.onCreate(savedInstanceState);
3957
setContentView(R.layout.activity_main);
4058

59+
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
60+
61+
// use this setting to improve performance if you know that changes
62+
// in content do not change the layout size of the RecyclerView
63+
mRecyclerView.setHasFixedSize(true);
64+
65+
// use a linear layout manager
66+
//mLayoutManager = new LinearLayoutManager(this);
67+
//mRecyclerView.setLayoutManager(mLayoutManager);
68+
69+
myDataset = new HashMap<String, Object>();
70+
// specify an adapter (see also next example)
71+
mAdapter = new MyAdapter(myDataset);
72+
mRecyclerView.setAdapter(mAdapter);
73+
4174
client = new AsyncHttpClient();
42-
client.get(API_PATH, new AsyncHttpResponseHandler() {
75+
client.get(TOP_CLASS_PATH, new AsyncHttpResponseHandler() {
4376
@Override
4477
public void onStart() {
4578
// called before request is started
@@ -51,6 +84,17 @@ public void onSuccess(int statusCode, Header[] headers, byte[] response) {
5184
// called when response HTTP status is "200 OK"
5285
Log.i(TAG, response.toString());
5386
System.out.println(response.toString());
87+
88+
if (response != null && response.length > 0) {
89+
ObjectMapper mapper = new ObjectMapper();
90+
TypeFactory typeFactory = mapper.getTypeFactory();
91+
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Object.class);
92+
try {
93+
myDataset = mapper.readValue(response.toString(), mapType);
94+
} catch (IOException e) {
95+
e.printStackTrace();
96+
}
97+
}
5498
}
5599

56100
@Override
@@ -83,7 +127,7 @@ public void onClick(View arg0) {
83127

84128
Log.i(TAG, "clicked");
85129

86-
client.get(API_PATH, new AsyncHttpResponseHandler() {
130+
client.get(APIS_PATH, new AsyncHttpResponseHandler() {
87131
@Override
88132
public void onStart() {
89133
// called before request is started
@@ -161,7 +205,7 @@ protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStat
161205
protected String doInBackground(Void... params) {
162206
HttpClient httpClient = new DefaultHttpClient();
163207
HttpContext localContext = new BasicHttpContext();
164-
HttpGet httpGet = new HttpGet(API_PATH);
208+
HttpGet httpGet = new HttpGet(APIS_PATH);
165209
String text = null;
166210
try {
167211
HttpResponse response = httpClient.execute(httpGet, localContext);
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package io.stackdocker.mooc.andapp;
2+
3+
import android.support.v7.widget.RecyclerView;
4+
import android.view.LayoutInflater;
5+
import android.view.ViewGroup;
6+
import android.widget.TextView;
7+
8+
import java.util.HashMap;
9+
10+
/**
11+
* Created by fanhongling on 18/3/25.
12+
*/
13+
14+
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
15+
private HashMap<String, Object> mDataset;
16+
17+
// Provide a reference to the views for each data item
18+
// Complex data items may need more than one view per item, and
19+
// you provide access to all the views for a data item in a view holder
20+
public static class ViewHolder extends RecyclerView.ViewHolder {
21+
// each data item is just a string in this case
22+
public TextView mTextView;
23+
public ViewHolder(TextView v) {
24+
super(v);
25+
mTextView = v;
26+
}
27+
}
28+
29+
// Provide a suitable constructor (depends on the kind of dataset)
30+
public MyAdapter(HashMap<String, Object> myDataset) {
31+
mDataset = myDataset;
32+
}
33+
34+
// Create new views (invoked by the layout manager)
35+
@Override
36+
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
37+
int viewType) {
38+
// create a new view
39+
TextView v = (TextView) LayoutInflater.from(parent.getContext())
40+
.inflate(R.layout.my_text_view, parent, false);
41+
42+
ViewHolder vh = new ViewHolder(v);
43+
return vh;
44+
}
45+
46+
// Replace the contents of a view (invoked by the layout manager)
47+
@Override
48+
public void onBindViewHolder(ViewHolder holder, int position) {
49+
// - get element from your dataset at this position
50+
// - replace the contents of the view with that element
51+
holder.mTextView.setText(mDataset.get("name").toString());
52+
53+
}
54+
55+
// Return the size of your dataset (invoked by the layout manager)
56+
@Override
57+
public int getItemCount() {
58+
return mDataset.size();
59+
}
60+
}

android/mooc/androidapp/app/src/main/res/layout/activity_main.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
tools:context="io.stackdocker.mooc.andapp.MainActivity">
88

99
<TextView
10+
android:id="@+id/my_text_view"
1011
android:layout_width="wrap_content"
1112
android:layout_height="wrap_content"
1213
android:text="Hello World!"
@@ -38,5 +39,28 @@
3839
android:id="@+id/my_edit"/>
3940

4041

42+
<!--<android.support.v7.widget.RecyclerView-->
43+
<!--android:id="@+id/my_recycler_view"-->
44+
<!--android:layout_width="0dp"-->
45+
<!--android:layout_height="0dp"-->
46+
<!--android:layout_marginBottom="0dp"-->
47+
<!--android:layout_marginLeft="0dp"-->
48+
<!--android:layout_marginRight="0dp"-->
49+
<!--android:layout_marginTop="0dp"-->
50+
<!--app:layout_constraintBottom_toBottomOf="parent"-->
51+
<!--app:layout_constraintLeft_toLeftOf="parent"-->
52+
<!--app:layout_constraintRight_toRightOf="parent"-->
53+
<!--app:layout_constraintTop_toTopOf="parent"/>-->
54+
<android.support.v7.widget.RecyclerView
55+
android:id="@+id/my_recycler_view"
56+
android:scrollbars="vertical"
57+
android:layout_width="match_parent"
58+
android:layout_height="match_parent"/>
59+
60+
<FrameLayout
61+
android:id="@+id/my_headlines_fragment"
62+
android:layout_weight="2"
63+
android:layout_width="match_parent"
64+
android:layout_height="0px" />
4165

4266
</android.support.constraint.ConstraintLayout>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent" android:layout_height="match_parent">
4+
5+
<TextView
6+
android:id="@+id/my_text_view"
7+
android:layout_width="wrap_content"
8+
android:layout_height="wrap_content"
9+
android:text="@string/my_text_view_text"/>
10+
11+
</FrameLayout>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<dimen name="text_margin">16dp</dimen>
4+
</resources>

android/mooc/androidapp/app/src/main/res/values/strings.xml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@
4949
<string name="title_use_pool_thread">Use Pool Thread in Response</string>
5050
<string name="title_request_params_debug">Request Params debug</string>
5151

52+
<string name="api_host">http://192.168.0.20:8080</string>
53+
<string name="top_classes_path">top-classes</string>
54+
<string name="top_coaches_path">top-coaches</string>
55+
<string name="top_practices_path">top-pratices</string>
56+
<string name="top_trainees_path">top-trainees</string>
57+
<string name="top_hires_path">top-hires</string>
58+
<string name="top_hunters_path">top-hunters</string>
59+
60+
<!-- TODO: Remove or change this placeholder text -->
61+
<string name="my_text_view_text">Hello text view</string>
62+
5263
</resources>

0 commit comments

Comments
 (0)