1
1
package com .loopj .android .http .sample ;
2
2
3
3
import android .app .Activity ;
4
+ import android .graphics .Color ;
4
5
import android .os .Bundle ;
5
6
import android .text .InputType ;
6
7
import android .util .Log ;
10
11
import android .widget .EditText ;
11
12
import android .widget .LinearLayout ;
12
13
import android .widget .ScrollView ;
14
+ import android .widget .TextView ;
13
15
14
16
import com .loopj .android .http .AsyncHttpClient ;
15
17
import com .loopj .android .http .AsyncHttpResponseHandler ;
16
18
17
19
import org .apache .http .Header ;
18
20
21
+ import java .io .PrintWriter ;
22
+ import java .io .StringWriter ;
23
+
19
24
public abstract class SampleParentActivity extends Activity {
20
25
21
26
private LinearLayout headers ; // Sample header, inputs and buttons
22
27
private LinearLayout contents ; // Sample output, states, errors, ...
23
28
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient ();
24
29
private EditText urlEditText ;
25
- private Button executeButton ;
26
30
private static final LinearLayout .LayoutParams lParams = new LinearLayout .LayoutParams (ViewGroup .LayoutParams .MATCH_PARENT , ViewGroup .LayoutParams .WRAP_CONTENT );
27
31
32
+ private static final int LIGHTGREEN = Color .parseColor ("#00FF66" );
33
+ private static final int LIGHTRED = Color .parseColor ("#FF3300" );
34
+ private static final int YELLOW = Color .parseColor ("#FFFF00" );
35
+ private static final int LIGHTBLUE = Color .parseColor ("#99CCFF" );
36
+
28
37
@ Override
29
38
protected void onCreate (Bundle savedInstanceState ) {
30
39
super .onCreate (savedInstanceState );
@@ -58,7 +67,7 @@ private void setupHeaders() {
58
67
urlEditText .setInputType (InputType .TYPE_CLASS_TEXT | InputType .TYPE_TEXT_VARIATION_URI );
59
68
urlEditText .setLayoutParams (new LinearLayout .LayoutParams (0 , ViewGroup .LayoutParams .WRAP_CONTENT , 1f ));
60
69
urlLayout .addView (urlEditText );
61
- executeButton = new Button (this );
70
+ Button executeButton = new Button (this );
62
71
executeButton .setText ("Run" );
63
72
executeButton .setLayoutParams (new LinearLayout .LayoutParams (ViewGroup .LayoutParams .WRAP_CONTENT , ViewGroup .LayoutParams .WRAP_CONTENT ));
64
73
urlLayout .addView (executeButton );
@@ -71,30 +80,68 @@ private void setupHeaders() {
71
80
executeButton .setOnClickListener (new View .OnClickListener () {
72
81
@ Override
73
82
public void onClick (View v ) {
74
- executeSample ();
83
+ executeSample (getAsyncHttpClient (), ( urlEditText == null || urlEditText . getText () == null ) ? getDefaultURL () : urlEditText . getText (). toString (), getResponseHandler () );
75
84
}
76
85
});
77
86
}
78
87
79
- protected abstract void executeSample ();
80
-
81
88
protected final void debugHeaders (String TAG , Header [] headers ) {
82
89
if (headers != null ) {
83
90
Log .d (TAG , "Return Headers:" );
91
+ StringBuilder builder = new StringBuilder ();
84
92
for (Header h : headers ) {
85
- Log .d (TAG , String .format ("%s : %s" , h .getName (), h .getValue ()));
93
+ String _h = String .format ("%s : %s" , h .getName (), h .getValue ());
94
+ Log .d (TAG , _h );
95
+ builder .append (_h );
96
+ builder .append ("\n " );
86
97
}
98
+ addView (getColoredView (YELLOW , builder .toString ()));
87
99
}
88
100
}
89
101
102
+ protected static String throwableToString (Throwable t ) {
103
+ if (t == null )
104
+ return null ;
105
+ StringWriter sw = new StringWriter ();
106
+ PrintWriter pw = new PrintWriter (sw );
107
+ t .printStackTrace (pw );
108
+ return pw .toString ();
109
+ }
110
+
90
111
protected final void debugThrowable (String TAG , Throwable t ) {
91
112
if (t != null ) {
92
113
Log .e (TAG , "AsyncHttpClient returned error" , t );
114
+ addView (getColoredView (LIGHTRED , throwableToString (t )));
115
+ }
116
+ }
117
+
118
+ protected final void debugResponse (String TAG , String response ) {
119
+ if (response != null ) {
120
+ Log .d (TAG , "Response data:" );
121
+ Log .d (TAG , response );
122
+ addView (getColoredView (LIGHTGREEN , response ));
93
123
}
94
124
}
95
125
96
126
protected final void debugStatusCode (String TAG , int statusCode ) {
97
- Log .d (TAG , String .format ("Return Status Code: %d" , statusCode ));
127
+ String msg = String .format ("Return Status Code: %d" , statusCode );
128
+ Log .d (TAG , msg );
129
+ addView (getColoredView (LIGHTBLUE , msg ));
130
+ }
131
+
132
+ public static int getContrastColor (int color ) {
133
+ double y = (299 * Color .red (color ) + 587 * Color .green (color ) + 114 * Color .blue (color )) / 1000 ;
134
+ return y >= 128 ? Color .BLACK : Color .WHITE ;
135
+ }
136
+
137
+ private View getColoredView (int bgColor , String msg ) {
138
+ TextView tv = new TextView (this );
139
+ tv .setLayoutParams (lParams );
140
+ tv .setText (msg );
141
+ tv .setBackgroundColor (bgColor );
142
+ tv .setPadding (10 , 10 , 10 , 10 );
143
+ tv .setTextColor (getContrastColor (bgColor ));
144
+ return tv ;
98
145
}
99
146
100
147
protected final void addView (View v ) {
@@ -118,4 +165,6 @@ protected final void clearOutputs() {
118
165
protected AsyncHttpClient getAsyncHttpClient () {
119
166
return this .asyncHttpClient ;
120
167
}
168
+
169
+ protected abstract void executeSample (AsyncHttpClient client , String URL , AsyncHttpResponseHandler responseHandler );
121
170
}
0 commit comments