Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Added ad hoc SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
daleharvey committed Aug 12, 2011
1 parent 4a37b1a commit 566554f
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 1 deletion.
128 changes: 128 additions & 0 deletions src/com/arandomurl/couchapp/AndCouch.java
@@ -0,0 +1,128 @@
package com.arandomurl.couchapp;

/*
* AndCouch is a very simple http wrapper library for CouchDB with minimal
* dependencies
*
*/

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

public class AndCouch {

public String[][] headers;
public JSONObject json;
public String result;
public int status;

public AndCouch(String[][] headers, JSONObject json, String result,
int status) {
this.headers = headers;
this.json = json;
this.result = result;
this.status = status;
}

public static AndCouch post(String url, String data) throws JSONException {
return post(url, data, new String[][]{});
}

public static AndCouch post(String url, String data, String[][] headers)
throws JSONException {
return AndCouch.httpRequest("POST", url, data, headers);
}

public static AndCouch put(String url, String data) throws JSONException {
return put(url, data, new String[][]{});
}

public static AndCouch put(String url, String data, String[][] headers)
throws JSONException {
return AndCouch.httpRequest("PUT", url, data, headers);
}

public static AndCouch get(String url) throws JSONException {
return get(url, new String[][] {});
}

public static AndCouch get(String url, String[][] headers)
throws JSONException {
return AndCouch.httpRequest("GET", url, null, headers);
}

public static AndCouch httpRequest(String method, String url,
String data, String[][] headers) throws JSONException {

StringBuffer sb = new StringBuffer();
int statusCode = 0;

try {

HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
String charEncoding = "iso-8859-1";

c.setDoOutput(true);
c.setUseCaches(false);

c.setRequestMethod(method);
c.setRequestProperty("Content-type", "application/json; charset="
+ "UTF-8");

for (String[] tmp : headers) {
c.setRequestProperty(tmp[0], tmp[1]);
}

if (method != "GET" && data != null) {
c.setDoInput(true);
c.setRequestProperty("Content-Length",
Integer.toString(data.length()));
c.getOutputStream().write(data.getBytes(charEncoding));
}

c.connect();

statusCode = c.getResponseCode();

// TODO: Nasty
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (FileNotFoundException e) {

} catch (NullPointerException e) {

}

finally {
c.disconnect();
}

} catch (IOException e) {
e.printStackTrace();
}

JSONObject json= sb.toString().length() == 0
? new JSONObject()
: new JSONObject(sb.toString());

return new AndCouch(headers, json, sb.toString(), statusCode);
};

@Override
public String toString() {
return "HTTPResult -> status: " + Integer.toString(status);
}
}
1 change: 0 additions & 1 deletion src/com/arandomurl/couchapp/CouchAppActivity.java
Expand Up @@ -25,7 +25,6 @@
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.couchbase.libcouch.AndCouch;
import com.couchbase.libcouch.CouchDB;
import com.couchbase.libcouch.ICouchClient;

Expand Down
122 changes: 122 additions & 0 deletions src/com/arandomurl/couchapp/HTTPRequest.java
@@ -0,0 +1,122 @@
package com.arandomurl.couchapp;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

public class HTTPRequest {

public String[][] headers;
public JSONObject json;
public String result;
public int status;

public HTTPRequest(String[][] headers, JSONObject json, String result,
int status) {
this.headers = headers;
this.json = json;
this.result = result;
this.status = status;
}

public static HTTPRequest post(String url, String data) throws JSONException {
return post(url, data, new String[][]{});
}

public static HTTPRequest post(String url, String data, String[][] headers)
throws JSONException {
return HTTPRequest.httpRequest("POST", url, data, headers);
}

public static HTTPRequest put(String url, String data) throws JSONException {
return put(url, data, new String[][]{});
}

public static HTTPRequest put(String url, String data, String[][] headers)
throws JSONException {
return HTTPRequest.httpRequest("PUT", url, data, headers);
}

public static HTTPRequest get(String url) throws JSONException {
return get(url, new String[][] {});
}

public static HTTPRequest get(String url, String[][] headers)
throws JSONException {
return HTTPRequest.httpRequest("GET", url, null, headers);
}

public static HTTPRequest httpRequest(String method, String url,
String data, String[][] headers) throws JSONException {

StringBuffer sb = new StringBuffer();
int statusCode = 0;

try {

HttpURLConnection c = (HttpURLConnection) new URL(url).openConnection();
String charEncoding = "iso-8859-1";

c.setDoOutput(true);
c.setUseCaches(false);

c.setRequestMethod(method);
c.setRequestProperty("Content-type", "application/json; charset="
+ "UTF-8");

for (String[] tmp : headers) {
c.setRequestProperty(tmp[0], tmp[1]);
}

if (method != "GET" && data != null) {
c.setDoInput(true);
c.setRequestProperty("Content-Length",
Integer.toString(data.length()));
c.getOutputStream().write(data.getBytes(charEncoding));
}

c.connect();

statusCode = c.getResponseCode();

// TODO: Nasty
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(
c.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
}
rd.close();
} catch (FileNotFoundException e) {

} catch (NullPointerException e) {

}

finally {
c.disconnect();
}

} catch (IOException e) {
e.printStackTrace();
}

JSONObject json= sb.toString().length() == 0
? new JSONObject()
: new JSONObject(sb.toString());

return new HTTPRequest(headers, json, sb.toString(), statusCode);
};

@Override
public String toString() {
return "HTTPResult -> status: " + Integer.toString(status);
}
}

0 comments on commit 566554f

Please sign in to comment.