Skip to content

Instantly share code, notes, and snippets.

@zonyitoo
Created December 9, 2012 13:55
Show Gist options
  • Save zonyitoo/4244970 to your computer and use it in GitHub Desktop.
Save zonyitoo/4244970 to your computer and use it in GitHub Desktop.
SYSU Argo api JAVA
package edu.sysu.bbs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import org.json.JSONException;
import org.json.JSONObject;
public class Argo {
private static Argo instance = null;
private static String SERVER = "http://bbs.sysu.edu.cn";
public static Argo getInstance() {
if (instance == null)
instance = new Argo();
return instance;
}
public JSONObject get(String apiurl, Map<String, Object> param) {
try {
URL url = new URL(SERVER + apiurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setFollowRedirects(true);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
PrintWriter pWriter = new PrintWriter(connection.getOutputStream());
JSONObject object = new JSONObject(param);
pWriter.print(object.toString());
pWriter.flush();
pWriter.close();
if (connection.getResponseCode() == 200) {
InputStreamReader isReader = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(isReader);
JSONObject obj = new JSONObject(br.readLine());
return obj;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public JSONObject post(String apiurl, Map<String, Object> param) {
try {
URL url = new URL(SERVER + apiurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setFollowRedirects(true);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
PrintWriter pWriter = new PrintWriter(connection.getOutputStream());
JSONObject object = new JSONObject(param);
pWriter.print(object.toString());
pWriter.flush();
pWriter.close();
if (connection.getResponseCode() == 200) {
InputStreamReader isReader = new InputStreamReader(connection.getInputStream());
BufferedReader br = new BufferedReader(isReader);
return new JSONObject(br.readLine());
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
import java.util.HashMap;
import org.json.JSONObject;
import edu.sysu.bbs.Argo;
public class ArgoTest {
public static void main(String[] argv) {
Argo argo = Argo.getInstance();
JSONObject object = argo.get("/ajax/board/all/", new HashMap<String, Object>());
System.out.println(object.toString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment