Skip to content

Commit

Permalink
Support HTTP PUT.
Browse files Browse the repository at this point in the history
  • Loading branch information
androidquery committed Aug 29, 2012
1 parent 0776a9a commit 384d1f6
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 1 addition & 1 deletion dist/build-full.jardesc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="WINDOWS-1252" standalone="no"?>
<jardesc>
<jar path="AndroidQuery/dist/android-query-full.0.23.10-beta.jar"/>
<jar path="AndroidQuery/dist/android-query-full.0.23.12-beta.jar"/>
<options buildIfNeeded="true" compress="true" descriptionLocation="/AndroidQuery/dist/build-full.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="true" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
<storedRefactorings deprecationInfo="true" structuralOnly="false"/>
<selectedProjects/>
Expand Down
11 changes: 11 additions & 0 deletions src/com/androidquery/AbstractAQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Map;
import java.util.WeakHashMap;

import org.apache.http.HttpEntity;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
Expand Down Expand Up @@ -1942,6 +1944,15 @@ public <K> T delete(String url, Class<K> type, AjaxCallback<K> callback){

}

public <K> T put(String url, String contentHeader, HttpEntity entity, Class<K> type, AjaxCallback<K> callback){

callback.url(url).type(type).method(AQuery.METHOD_PUT).header("Content-Type", contentHeader).param(AQuery.POST_ENTITY, entity);
return ajax(callback);

}



/**
* Ajax HTTP delete.
*
Expand Down
29 changes: 26 additions & 3 deletions src/com/androidquery/callback/AbstractAjaxCallback.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.conn.params.ConnManagerParams;
Expand Down Expand Up @@ -1174,6 +1177,8 @@ private void network() throws IOException{

if(Constants.METHOD_DELETE == method){
httpDelete(url, headers, status);
}else if(Constants.METHOD_PUT == method){
httpPut(url, headers, params, status);
}else if(params == null){
httpGet(url, headers, status);
}else{
Expand Down Expand Up @@ -1271,8 +1276,26 @@ private void httpPost(String url, Map<String, String> headers, Map<String, Objec

AQUtility.debug("post", url);

HttpEntityEnclosingRequestBase req = new HttpPost(url);

HttpPost post = new HttpPost(url);
httpEntity(url, req, headers, params, status);

}

private void httpPut(String url, Map<String, String> headers, Map<String, Object> params, AjaxStatus status) throws ClientProtocolException, IOException{

AQUtility.debug("put", url);

HttpEntityEnclosingRequestBase req = new HttpPut(url);

httpEntity(url, req, headers, params, status);

}


private void httpEntity(String url, HttpEntityEnclosingRequestBase req, Map<String, String> headers, Map<String, Object> params, AjaxStatus status) throws ClientProtocolException, IOException{

//HttpEntityEnclosingRequestBase req = new HttpPost(url);

HttpEntity entity = null;

Expand Down Expand Up @@ -1300,8 +1323,8 @@ private void httpPost(String url, Map<String, String> headers, Map<String, Objec
headers.put("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
}

post.setEntity(entity);
httpDo(post, url, headers, status);
req.setEntity(entity);
httpDo(req, url, headers, status);


}
Expand Down
4 changes: 2 additions & 2 deletions src/com/androidquery/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public interface Constants {


public static final String VERSION = "0.23.11";
public static final String VERSION = "0.23.12";

public static final int LAYER_TYPE_SOFTWARE = 1;
public static final int LAYER_TYPE_HARDWARE = 2;
Expand All @@ -41,7 +41,7 @@ public interface Constants {
public static final int METHOD_GET = 0;
public static final int METHOD_POST = 1;
public static final int METHOD_DELETE = 2;
//public static final int METHOD_PUT = 3;
public static final int METHOD_PUT = 3;

public static final int TAG_URL = 0x40FF0001;
public static final int TAG_SCROLL_LISTENER = 0x40FF0002;
Expand Down
33 changes: 33 additions & 0 deletions tests/src/com/androidquery/test/AQueryAsyncTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
Expand Down Expand Up @@ -1411,6 +1412,38 @@ public void callback(String url, JSONObject jo, AjaxStatus status) {

}


public void testAjaxPut() throws UnsupportedEncodingException{

String url = "http://www.androidquery.com/p/doNothing";

AjaxCallback<JSONObject> cb = new AjaxCallback<JSONObject>(){

@Override
public void callback(String url, JSONObject jo, AjaxStatus status) {

done(url, jo, status);

}

};

StringEntity entity = new StringEntity(new JSONObject().toString());

aq.put(url, "application/json", entity, JSONObject.class, cb);

waitAsync();

JSONObject jo = (JSONObject) result;

AQUtility.debug(jo);

assertNotNull(jo);

assertEquals("PUT", jo.optString("method"));

}

public void testAjaxTimeout() {

String url = "http://farm6.static.flickr.com/5035/5802797131_a729dac808_b.jpg";
Expand Down

0 comments on commit 384d1f6

Please sign in to comment.