Add some demo REST client code for Java.
This commit is contained in:
parent
9def829046
commit
3c00d669ba
BIN
client/Java/Demo.class
Normal file
BIN
client/Java/Demo.class
Normal file
Binary file not shown.
50
client/Java/Demo.java
Normal file
50
client/Java/Demo.java
Normal file
@ -0,0 +1,50 @@
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import org.apache.http.HttpEntity;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.cookie.Cookie;
|
||||
import org.apache.http.impl.client.DefaultHttpClient;
|
||||
import org.apache.http.message.BasicNameValuePair;
|
||||
import org.apache.http.protocol.HTTP;
|
||||
|
||||
public class Demo {
|
||||
public static final String BASE_URL = "http://example.com/gallery3/index.php";
|
||||
public static final String USERNAME = "admin";
|
||||
public static final String PASSWORD = "admin";
|
||||
|
||||
public static void main(String[] argv) throws java.io.UnsupportedEncodingException, java.io.IOException {
|
||||
DefaultHttpClient httpclient = new DefaultHttpClient();
|
||||
HttpResponse response;
|
||||
Gson gson = new Gson();
|
||||
|
||||
// Get the REST API key
|
||||
HttpPost post = new HttpPost(BASE_URL + "/rest");
|
||||
ArrayList<NameValuePair> nvps = new ArrayList <NameValuePair>();
|
||||
nvps.add(new BasicNameValuePair("user", USERNAME));
|
||||
nvps.add(new BasicNameValuePair("password", USERNAME));
|
||||
post.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
|
||||
response = httpclient.execute(post);
|
||||
String api_key = gson.fromJson(new BufferedReader(
|
||||
new InputStreamReader(response.getEntity().getContent())).readLine(), String.class);
|
||||
System.out.println("API Key:" + api_key);
|
||||
|
||||
// Get the JSON representation of the root album, which we know has id 1
|
||||
HttpGet get = new HttpGet(BASE_URL + "/rest/item/1");
|
||||
get.setHeader("X-Gallery-Request-Method", "GET");
|
||||
get.setHeader("X-Gallery-Request-Key", api_key);
|
||||
response = httpclient.execute(get);
|
||||
|
||||
System.out.println(
|
||||
"Response: " + new BufferedReader(new InputStreamReader(response.getEntity().getContent())).readLine());
|
||||
}
|
||||
}
|
10
client/Java/README
Normal file
10
client/Java/README
Normal file
@ -0,0 +1,10 @@
|
||||
This is very, very rough sample code for how to do some Java REST
|
||||
requests. To run this code:
|
||||
|
||||
1) Edit Demo.java and set the BASE_URL to your Gallery3 install.
|
||||
2) Edit USERNAME and PASSWORD as appropriate
|
||||
3) In a shell, do "sh build.sh"
|
||||
4) In a shell, do "sh run.sh"
|
||||
|
||||
Note that there is NO error checking, so if something goes wrong
|
||||
you'll have to debug it.
|
1
client/Java/build.sh
Normal file
1
client/Java/build.sh
Normal file
@ -0,0 +1 @@
|
||||
javac -classpath lib/httpclient-4.0.1.jar:lib/httpcore-4.0.1.jar:lib/gson-1.4.jar Demo.java
|
BIN
client/Java/lib/commons-logging-api-1.1.1.jar
Normal file
BIN
client/Java/lib/commons-logging-api-1.1.1.jar
Normal file
Binary file not shown.
BIN
client/Java/lib/gson-1.4.jar
Normal file
BIN
client/Java/lib/gson-1.4.jar
Normal file
Binary file not shown.
BIN
client/Java/lib/httpclient-4.0.1.jar
Normal file
BIN
client/Java/lib/httpclient-4.0.1.jar
Normal file
Binary file not shown.
BIN
client/Java/lib/httpcore-4.0.1.jar
Normal file
BIN
client/Java/lib/httpcore-4.0.1.jar
Normal file
Binary file not shown.
1
client/Java/run.sh
Normal file
1
client/Java/run.sh
Normal file
@ -0,0 +1 @@
|
||||
java -classpath lib/httpclient-4.0.1.jar:lib/httpcore-4.0.1.jar:lib/commons-logging-api-1.1.1.jar:lib/gson-1.4.jar:. Demo
|
Reference in New Issue
Block a user