Skip to content

Commit b5dae2c

Browse files
committed
Added Digest Auth sample, added PrePostProcessingSample into Manifest
1 parent 2a6a722 commit b5dae2c

File tree

7 files changed

+97
-1
lines changed

7 files changed

+97
-1
lines changed

sample/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
<activity android:name=".AsyncBackgroundThreadSample"/>
4343
<activity android:name=".ContentTypeForHttpEntitySample"/>
4444
<activity android:name=".ResumeDownloadSample"/>
45+
<activity android:name=".PrePostProcessingSample"/>
46+
<activity android:name=".DigestAuthSample"/>
4547

4648
<service android:name=".services.ExampleIntentService"/>
4749
</application>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.loopj.android.http.sample;
2+
3+
import android.net.Uri;
4+
import android.os.Bundle;
5+
import android.widget.EditText;
6+
7+
import com.loopj.android.http.AsyncHttpClient;
8+
import com.loopj.android.http.RequestHandle;
9+
import com.loopj.android.http.ResponseHandlerInterface;
10+
11+
import org.apache.http.Header;
12+
import org.apache.http.HttpEntity;
13+
import org.apache.http.auth.AuthScope;
14+
import org.apache.http.auth.UsernamePasswordCredentials;
15+
16+
public class DigestAuthSample extends GetSample {
17+
18+
private EditText usernameField, passwordField;
19+
20+
@Override
21+
public String getDefaultURL() {
22+
return PROTOCOL + "httpbin.org/digest-auth/auth/user/passwd2";
23+
}
24+
25+
@Override
26+
public int getSampleTitle() {
27+
return R.string.title_digest_auth;
28+
}
29+
30+
@Override
31+
protected void onCreate(Bundle savedInstanceState) {
32+
super.onCreate(savedInstanceState);
33+
usernameField = new EditText(this);
34+
passwordField = new EditText(this);
35+
usernameField.setHint("Username");
36+
passwordField.setHint("Password");
37+
usernameField.setText("user");
38+
passwordField.setText("passwd2");
39+
customFieldsLayout.addView(usernameField);
40+
customFieldsLayout.addView(passwordField);
41+
}
42+
43+
@Override
44+
public RequestHandle executeSample(AsyncHttpClient client, String URL, Header[] headers, HttpEntity entity, ResponseHandlerInterface responseHandler) {
45+
setCredentials(client, URL);
46+
return client.get(this, URL, headers, null, responseHandler);
47+
}
48+
49+
@Override
50+
public boolean isCancelButtonAllowed() {
51+
return true;
52+
}
53+
54+
@Override
55+
public boolean isRequestHeadersAllowed() {
56+
return true;
57+
}
58+
59+
@Override
60+
public boolean isRequestBodyAllowed() {
61+
return false;
62+
}
63+
64+
private void setCredentials(AsyncHttpClient client, String URL) {
65+
Uri parsed = Uri.parse(URL);
66+
client.clearCredentialsProvider();
67+
client.setCredentials(
68+
new AuthScope(parsed.getHost(), parsed.getPort() == -1 ? 80 : parsed.getPort()),
69+
new UsernamePasswordCredentials(
70+
usernameField.getText().toString(),
71+
passwordField.getText().toString()
72+
)
73+
);
74+
}
75+
}

sample/src/main/java/com/loopj/android/http/sample/SampleParentActivity.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ protected AsyncHttpRequest newAsyncHttpRequest(DefaultHttpClient client, HttpCon
6969
};
7070
private EditText urlEditText, headersEditText, bodyEditText;
7171
private LinearLayout responseLayout;
72+
public LinearLayout customFieldsLayout;
7273
private final List<RequestHandle> requestHandles = new LinkedList<RequestHandle>();
7374
private static final String LOG_TAG = "SampleParentActivity";
7475

@@ -97,6 +98,7 @@ protected void onCreate(Bundle savedInstanceState) {
9798
urlEditText = (EditText) findViewById(R.id.edit_url);
9899
headersEditText = (EditText) findViewById(R.id.edit_headers);
99100
bodyEditText = (EditText) findViewById(R.id.edit_body);
101+
customFieldsLayout = (LinearLayout) findViewById(R.id.layout_custom);
100102
Button runButton = (Button) findViewById(R.id.button_run);
101103
Button cancelButton = (Button) findViewById(R.id.button_cancel);
102104
LinearLayout headersLayout = (LinearLayout) findViewById(R.id.layout_headers);
@@ -142,6 +144,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
142144
case MENU_USE_HTTPS:
143145
useHttps = !useHttps;
144146
PROTOCOL = useHttps ? PROTOCOL_HTTPS : PROTOCOL_HTTP;
147+
urlEditText.setText(getDefaultURL());
145148
return true;
146149
case MENU_CLEAR_VIEW:
147150
clearOutputs();

sample/src/main/java/com/loopj/android/http/sample/WaypointsActivity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ public class WaypointsActivity extends ListActivity {
5555
new SampleConfig(R.string.title_401_unauth, Http401AuthSample.class),
5656
new SampleConfig(R.string.title_pre_post_processing, PrePostProcessingSample.class),
5757
new SampleConfig(R.string.title_content_type_http_entity, ContentTypeForHttpEntitySample.class),
58-
new SampleConfig(R.string.title_resume_download, ResumeDownloadSample.class)
58+
new SampleConfig(R.string.title_resume_download, ResumeDownloadSample.class),
59+
new SampleConfig(R.string.title_digest_auth, DigestAuthSample.class)
5960
};
6061

6162
@Override

sample/src/main/res/layout-v14/parent_layout.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@
8484

8585
</LinearLayout>
8686

87+
<LinearLayout
88+
android:id="@+id/layout_custom"
89+
android:layout_width="fill_parent"
90+
android:layout_height="wrap_content"
91+
android:orientation="vertical">
92+
</LinearLayout>
93+
8794
<LinearLayout
8895
android:id="@+id/layout_response"
8996
android:layout_width="fill_parent"

sample/src/main/res/layout/parent_layout.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@
8282

8383
</LinearLayout>
8484

85+
<LinearLayout
86+
android:id="@+id/layout_custom"
87+
android:layout_width="fill_parent"
88+
android:layout_height="wrap_content"
89+
android:orientation="vertical">
90+
</LinearLayout>
91+
8592
<LinearLayout
8693
android:id="@+id/layout_response"
8794
android:layout_width="fill_parent"

sample/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,5 @@
4242
<string name="title_async_background_thread">Async on background thread</string>
4343
<string name="title_content_type_http_entity">Content-Type with HttpEntity</string>
4444
<string name="title_resume_download">Resuming Download</string>
45+
<string name="title_digest_auth">Digest Authentication</string>
4546
</resources>

0 commit comments

Comments
 (0)