public DemoAPI() { mOkHttpTool = new OkHttpTool(); gson = new Gson(); Map<String, String> defaultHeaders = new HashMap<String, String>(); defaultHeaders.put("Content-Type", "application/json"); mOkHttpTool.setDefaultHeaders(defaultHeaders); }
public Object postData(String url, String userId, String data) throws IOException { List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(); params.add(new BasicNameValuePair("userid", userId)); params.add(new BasicNameValuePair("data", data)); return parseResponse(mOkHttpTool.post(url, new UrlEncodedFormEntity(params)), Object.class); }
public Object getData(String url) throws IOException { return parseResponse(mOkHttpTool.get(url), Object.class); }
private <T> T parseResponse(HttpURLConnection connection, Type type) throws IOException { BufferedReader bufferedReader = mOkHttpTool.getBufferedInputReader(connection); T response = gson.fromJson(bufferedReader, type); connection.disconnect(); return response; }