-
Notifications
You must be signed in to change notification settings - Fork 103
/
UriRequestTask.java
83 lines (70 loc) · 2.59 KB
/
UriRequestTask.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.finchframework.finch.rest;
import android.content.Context;
import android.net.Uri;
import android.util.Log;
import com.finchframework.finch.Finch;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
/**
* Provides a runnable that uses an HttpClient to asynchronously load a given
* URI. After the network content is loaded, the task delegates handling of the
* request to a ResponseHandler specialized to handle the given content.
*/
public class UriRequestTask implements Runnable {
private HttpUriRequest mRequest;
private ResponseHandler mHandler;
protected Context mAppContext;
private RESTfulContentProvider mSiteProvider;
private String mRequestTag;
private int mRawResponse = -1;
// private int mRawResponse = R.raw.map_src;
public UriRequestTask(HttpUriRequest request,
ResponseHandler handler, Context appContext) {
this(null, null, request, handler, appContext);
}
public UriRequestTask(String requestTag,
RESTfulContentProvider siteProvider,
HttpUriRequest request,
ResponseHandler handler, Context appContext) {
mRequestTag = requestTag;
mSiteProvider = siteProvider;
mRequest = request;
mHandler = handler;
mAppContext = appContext;
}
public void setRawResponse(int rawResponse) {
mRawResponse = rawResponse;
}
/**
* Carries out the request on the complete URI as indicated by the protocol,
* host, and port contained in the configuration, and the URI supplied to
* the constructor.
*/
public void run() {
HttpResponse response;
try {
response = execute(mRequest);
mHandler.handleResponse(response, getUri());
} catch (IOException e) {
Log.w(Finch.LOG_TAG, "exception processing asynch request", e);
} finally {
if (mSiteProvider != null) {
mSiteProvider.requestComplete(mRequestTag);
}
}
}
private HttpResponse execute(HttpUriRequest mRequest) throws IOException {
if (mRawResponse >= 0) {
return new RawResponse(mAppContext, mRawResponse);
} else {
HttpClient client = new DefaultHttpClient();
return client.execute(mRequest);
}
}
public Uri getUri() {
return Uri.parse(mRequest.getURI().toString());
}
}