Skip to content

Commit c383108

Browse files
committed
Merge pull request android-async-http#141 from f-barth/master
Synchronous Http Client
2 parents 9636386 + 4e1cb7b commit c383108

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

src/com/loopj/android/http/AsyncHttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ public void delete(Context context, String url, Header[] headers, AsyncHttpRespo
548548

549549

550550
// Private stuff
551-
private void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
551+
protected void sendRequest(DefaultHttpClient client, HttpContext httpContext, HttpUriRequest uriRequest, String contentType, AsyncHttpResponseHandler responseHandler, Context context) {
552552
if(contentType != null) {
553553
uriRequest.addHeader("Content-Type", contentType);
554554
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package com.loopj.android.http;
2+
3+
import org.apache.http.client.methods.HttpUriRequest;
4+
import org.apache.http.impl.client.DefaultHttpClient;
5+
import org.apache.http.protocol.HttpContext;
6+
7+
import android.content.Context;
8+
import android.os.Message;
9+
10+
public abstract class SyncHttpClient extends AsyncHttpClient {
11+
private int responseCode;
12+
/*
13+
* as this is a synchronous request this is just a helping mechanism to pass
14+
* the result back to this method. Therefore the result object has to be a
15+
* field to be accessible
16+
*/
17+
private String result;
18+
AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {
19+
20+
void sendResponseMessage(org.apache.http.HttpResponse response) {
21+
responseCode = response.getStatusLine().getStatusCode();
22+
super.sendResponseMessage(response);
23+
};
24+
25+
@Override
26+
protected void sendMessage(Message msg) {
27+
/*
28+
* Dont use the handler and send it directly to the analysis
29+
* (because its all the same thread)
30+
*/
31+
handleMessage(msg);
32+
}
33+
34+
@Override
35+
public void onSuccess(String content) {
36+
result = content;
37+
}
38+
39+
@Override
40+
public void onFailure(Throwable error, String content) {
41+
result = onRequestFailed(error, content);
42+
}
43+
};
44+
45+
/**
46+
* @return the response code for the last request, might be usefull
47+
* sometimes
48+
*/
49+
public int getResponseCode() {
50+
return responseCode;
51+
}
52+
53+
// Private stuff
54+
protected void sendRequest(DefaultHttpClient client,
55+
HttpContext httpContext, HttpUriRequest uriRequest,
56+
String contentType, AsyncHttpResponseHandler responseHandler,
57+
Context context) {
58+
if (contentType != null) {
59+
uriRequest.addHeader("Content-Type", contentType);
60+
}
61+
62+
/*
63+
* will execute the request directly
64+
*/
65+
new AsyncHttpRequest(client, httpContext, uriRequest, responseHandler)
66+
.run();
67+
}
68+
69+
public abstract String onRequestFailed(Throwable error, String content);
70+
71+
public void delete(String url, RequestParams queryParams,
72+
AsyncHttpResponseHandler responseHandler) {
73+
// TODO what about query params??
74+
delete(url, responseHandler);
75+
}
76+
77+
public String get(String url, RequestParams params) {
78+
this.get(url, params, responseHandler);
79+
/*
80+
* the response handler will have set the result when this line is
81+
* reached
82+
*/
83+
return result;
84+
}
85+
86+
public String get(String url) {
87+
this.get(url, null, responseHandler);
88+
return result;
89+
}
90+
91+
public String put(String url, RequestParams params) {
92+
this.put(url, params, responseHandler);
93+
return result;
94+
}
95+
96+
public String put(String url) {
97+
this.put(url, null, responseHandler);
98+
return result;
99+
}
100+
101+
public String post(String url, RequestParams params) {
102+
this.post(url, params, responseHandler);
103+
return result;
104+
}
105+
106+
public String post(String url) {
107+
this.post(url, null, responseHandler);
108+
return result;
109+
}
110+
111+
public String delete(String url, RequestParams params) {
112+
this.delete(url, params, responseHandler);
113+
return result;
114+
}
115+
116+
public String delete(String url) {
117+
this.delete(url, null, responseHandler);
118+
return result;
119+
}
120+
121+
}

0 commit comments

Comments
 (0)