@Test public void requestSearchAPI() { String url = mServer.getUrl("/2013-09-01/classes/TestClass").toString(); NCMBRequest request = null; NCMBException error = null; try { request = new NCMBRequest(url, Constants.HTTP_METHOD_GET, null, new JSONObject("{'where':{'key':'value'}, 'limit':1}"), "sessionToken", "appKey", "clientKey"); } catch (NCMBException e) { error = e; } catch (JSONException e) { error = new NCMBException(NCMBException.INVALID_JSON, "Invalid json query"); } NCMBConnection connection = new NCMBConnection(request); NCMBResponse response = null; String assertObjectId = null; try { response = connection.sendRequest(); JSONArray results = response.responseData.getJSONArray("results"); JSONObject firstObj = results.getJSONObject(0); assertObjectId = firstObj.getString("objectId"); } catch (NCMBException e) { error = e; } catch (JSONException e) { e.printStackTrace(); } Assert.assertNull(error); Assert.assertEquals(200, response.statusCode); Assert.assertEquals("8FgKqFlH8dZRDrBJ", assertObjectId); }
public NCMBResponse sendRequest() throws NCMBException { NCMBResponse res = null; try { ExecutorService exec = Executors.newSingleThreadExecutor(); Future<NCMBResponse> future = exec.submit(new Callable<NCMBResponse>() { @Override public NCMBResponse call() throws NCMBException { HttpURLConnection urlConnection = null; NCMBResponse res = null; try { URL url = ncmbRequest.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(ncmbRequest.getMethod()); for (String requestKey : ncmbRequest.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, ncmbRequest.getRequestProperty(requestKey)); } if (urlConnection.getRequestMethod().equals("POST") || urlConnection.getRequestMethod().equals("PUT")) { urlConnection.setDoOutput(true); if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_JSON)) { DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(ncmbRequest.getContent()); writer.flush(); writer.close(); } else if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_FILE)) { final String boundary = Long.toString(System.currentTimeMillis()); final String lineEnd = "\r\n"; urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=file; filename=" + URLEncoder.encode(ncmbRequest.getFileName(), "UTF-8") + lineEnd); out.writeBytes("Content-Type: " + createMimeType(ncmbRequest.getFileName()) + lineEnd); out.writeBytes(lineEnd); for (int i = 0; i < ncmbRequest.getFileData().length; i++) { out.writeByte(ncmbRequest.getFileData()[i]); } out.writeBytes(lineEnd); if (!ncmbRequest.getContent().isEmpty() && !ncmbRequest.getContent().equals("{}")) { out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=acl; filename=acl" + lineEnd); out.writeBytes(lineEnd); out.writeBytes(ncmbRequest.getContent()); out.writeBytes(lineEnd); } out.writeBytes("--" + boundary + "--" + lineEnd); out.flush(); out.close(); } } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { res = new NCMBResponse(urlConnection.getInputStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } else { res = new NCMBResponse(urlConnection.getErrorStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } responseSignatureCheck(urlConnection, res, ncmbRequest); } catch (IOException e) { throw new NCMBException(NCMBException.AUTH_FAILURE, e.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return res; } }); res = future.get(sConnectionTimeout, TimeUnit.MILLISECONDS); if (res.statusCode != HttpURLConnection.HTTP_CREATED && res.statusCode != HttpURLConnection.HTTP_OK) { throw new NCMBException(res.mbStatus, res.mbErrorMessage); } return res; } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new NCMBException(e); } }
NCMBConnection { public NCMBResponse sendRequest() throws NCMBException { NCMBResponse res = null; try { ExecutorService exec = Executors.newSingleThreadExecutor(); Future<NCMBResponse> future = exec.submit(new Callable<NCMBResponse>() { @Override public NCMBResponse call() throws NCMBException { HttpURLConnection urlConnection = null; NCMBResponse res = null; try { URL url = ncmbRequest.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(ncmbRequest.getMethod()); for (String requestKey : ncmbRequest.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, ncmbRequest.getRequestProperty(requestKey)); } if (urlConnection.getRequestMethod().equals("POST") || urlConnection.getRequestMethod().equals("PUT")) { urlConnection.setDoOutput(true); if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_JSON)) { DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(ncmbRequest.getContent()); writer.flush(); writer.close(); } else if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_FILE)) { final String boundary = Long.toString(System.currentTimeMillis()); final String lineEnd = "\r\n"; urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=file; filename=" + URLEncoder.encode(ncmbRequest.getFileName(), "UTF-8") + lineEnd); out.writeBytes("Content-Type: " + createMimeType(ncmbRequest.getFileName()) + lineEnd); out.writeBytes(lineEnd); for (int i = 0; i < ncmbRequest.getFileData().length; i++) { out.writeByte(ncmbRequest.getFileData()[i]); } out.writeBytes(lineEnd); if (!ncmbRequest.getContent().isEmpty() && !ncmbRequest.getContent().equals("{}")) { out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=acl; filename=acl" + lineEnd); out.writeBytes(lineEnd); out.writeBytes(ncmbRequest.getContent()); out.writeBytes(lineEnd); } out.writeBytes("--" + boundary + "--" + lineEnd); out.flush(); out.close(); } } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { res = new NCMBResponse(urlConnection.getInputStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } else { res = new NCMBResponse(urlConnection.getErrorStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } responseSignatureCheck(urlConnection, res, ncmbRequest); } catch (IOException e) { throw new NCMBException(NCMBException.AUTH_FAILURE, e.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return res; } }); res = future.get(sConnectionTimeout, TimeUnit.MILLISECONDS); if (res.statusCode != HttpURLConnection.HTTP_CREATED && res.statusCode != HttpURLConnection.HTTP_OK) { throw new NCMBException(res.mbStatus, res.mbErrorMessage); } return res; } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new NCMBException(e); } } }
NCMBConnection { public NCMBResponse sendRequest() throws NCMBException { NCMBResponse res = null; try { ExecutorService exec = Executors.newSingleThreadExecutor(); Future<NCMBResponse> future = exec.submit(new Callable<NCMBResponse>() { @Override public NCMBResponse call() throws NCMBException { HttpURLConnection urlConnection = null; NCMBResponse res = null; try { URL url = ncmbRequest.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(ncmbRequest.getMethod()); for (String requestKey : ncmbRequest.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, ncmbRequest.getRequestProperty(requestKey)); } if (urlConnection.getRequestMethod().equals("POST") || urlConnection.getRequestMethod().equals("PUT")) { urlConnection.setDoOutput(true); if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_JSON)) { DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(ncmbRequest.getContent()); writer.flush(); writer.close(); } else if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_FILE)) { final String boundary = Long.toString(System.currentTimeMillis()); final String lineEnd = "\r\n"; urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=file; filename=" + URLEncoder.encode(ncmbRequest.getFileName(), "UTF-8") + lineEnd); out.writeBytes("Content-Type: " + createMimeType(ncmbRequest.getFileName()) + lineEnd); out.writeBytes(lineEnd); for (int i = 0; i < ncmbRequest.getFileData().length; i++) { out.writeByte(ncmbRequest.getFileData()[i]); } out.writeBytes(lineEnd); if (!ncmbRequest.getContent().isEmpty() && !ncmbRequest.getContent().equals("{}")) { out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=acl; filename=acl" + lineEnd); out.writeBytes(lineEnd); out.writeBytes(ncmbRequest.getContent()); out.writeBytes(lineEnd); } out.writeBytes("--" + boundary + "--" + lineEnd); out.flush(); out.close(); } } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { res = new NCMBResponse(urlConnection.getInputStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } else { res = new NCMBResponse(urlConnection.getErrorStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } responseSignatureCheck(urlConnection, res, ncmbRequest); } catch (IOException e) { throw new NCMBException(NCMBException.AUTH_FAILURE, e.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return res; } }); res = future.get(sConnectionTimeout, TimeUnit.MILLISECONDS); if (res.statusCode != HttpURLConnection.HTTP_CREATED && res.statusCode != HttpURLConnection.HTTP_OK) { throw new NCMBException(res.mbStatus, res.mbErrorMessage); } return res; } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new NCMBException(e); } } NCMBConnection(NCMBRequest request); }
NCMBConnection { public NCMBResponse sendRequest() throws NCMBException { NCMBResponse res = null; try { ExecutorService exec = Executors.newSingleThreadExecutor(); Future<NCMBResponse> future = exec.submit(new Callable<NCMBResponse>() { @Override public NCMBResponse call() throws NCMBException { HttpURLConnection urlConnection = null; NCMBResponse res = null; try { URL url = ncmbRequest.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(ncmbRequest.getMethod()); for (String requestKey : ncmbRequest.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, ncmbRequest.getRequestProperty(requestKey)); } if (urlConnection.getRequestMethod().equals("POST") || urlConnection.getRequestMethod().equals("PUT")) { urlConnection.setDoOutput(true); if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_JSON)) { DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(ncmbRequest.getContent()); writer.flush(); writer.close(); } else if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_FILE)) { final String boundary = Long.toString(System.currentTimeMillis()); final String lineEnd = "\r\n"; urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=file; filename=" + URLEncoder.encode(ncmbRequest.getFileName(), "UTF-8") + lineEnd); out.writeBytes("Content-Type: " + createMimeType(ncmbRequest.getFileName()) + lineEnd); out.writeBytes(lineEnd); for (int i = 0; i < ncmbRequest.getFileData().length; i++) { out.writeByte(ncmbRequest.getFileData()[i]); } out.writeBytes(lineEnd); if (!ncmbRequest.getContent().isEmpty() && !ncmbRequest.getContent().equals("{}")) { out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=acl; filename=acl" + lineEnd); out.writeBytes(lineEnd); out.writeBytes(ncmbRequest.getContent()); out.writeBytes(lineEnd); } out.writeBytes("--" + boundary + "--" + lineEnd); out.flush(); out.close(); } } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { res = new NCMBResponse(urlConnection.getInputStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } else { res = new NCMBResponse(urlConnection.getErrorStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } responseSignatureCheck(urlConnection, res, ncmbRequest); } catch (IOException e) { throw new NCMBException(NCMBException.AUTH_FAILURE, e.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return res; } }); res = future.get(sConnectionTimeout, TimeUnit.MILLISECONDS); if (res.statusCode != HttpURLConnection.HTTP_CREATED && res.statusCode != HttpURLConnection.HTTP_OK) { throw new NCMBException(res.mbStatus, res.mbErrorMessage); } return res; } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new NCMBException(e); } } NCMBConnection(NCMBRequest request); void setCallbackListener(RequestApiCallback callback); NCMBResponse sendRequest(); void sendRequestAsynchronously(RequestApiCallback callback); }
NCMBConnection { public NCMBResponse sendRequest() throws NCMBException { NCMBResponse res = null; try { ExecutorService exec = Executors.newSingleThreadExecutor(); Future<NCMBResponse> future = exec.submit(new Callable<NCMBResponse>() { @Override public NCMBResponse call() throws NCMBException { HttpURLConnection urlConnection = null; NCMBResponse res = null; try { URL url = ncmbRequest.getUrl(); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod(ncmbRequest.getMethod()); for (String requestKey : ncmbRequest.getAllRequestProperties().keySet()) { urlConnection.setRequestProperty(requestKey, ncmbRequest.getRequestProperty(requestKey)); } if (urlConnection.getRequestMethod().equals("POST") || urlConnection.getRequestMethod().equals("PUT")) { urlConnection.setDoOutput(true); if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_JSON)) { DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8")); writer.write(ncmbRequest.getContent()); writer.flush(); writer.close(); } else if (urlConnection.getRequestProperty("Content-Type").equals(NCMBRequest.HEADER_CONTENT_TYPE_FILE)) { final String boundary = Long.toString(System.currentTimeMillis()); final String lineEnd = "\r\n"; urlConnection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream()); out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=file; filename=" + URLEncoder.encode(ncmbRequest.getFileName(), "UTF-8") + lineEnd); out.writeBytes("Content-Type: " + createMimeType(ncmbRequest.getFileName()) + lineEnd); out.writeBytes(lineEnd); for (int i = 0; i < ncmbRequest.getFileData().length; i++) { out.writeByte(ncmbRequest.getFileData()[i]); } out.writeBytes(lineEnd); if (!ncmbRequest.getContent().isEmpty() && !ncmbRequest.getContent().equals("{}")) { out.writeBytes("--" + boundary + lineEnd); out.writeBytes("Content-Disposition: form-data; name=acl; filename=acl" + lineEnd); out.writeBytes(lineEnd); out.writeBytes(ncmbRequest.getContent()); out.writeBytes(lineEnd); } out.writeBytes("--" + boundary + "--" + lineEnd); out.flush(); out.close(); } } urlConnection.connect(); if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_CREATED || urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { res = new NCMBResponse(urlConnection.getInputStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } else { res = new NCMBResponse(urlConnection.getErrorStream(), urlConnection.getResponseCode(), urlConnection.getHeaderFields()); } responseSignatureCheck(urlConnection, res, ncmbRequest); } catch (IOException e) { throw new NCMBException(NCMBException.AUTH_FAILURE, e.getMessage()); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } return res; } }); res = future.get(sConnectionTimeout, TimeUnit.MILLISECONDS); if (res.statusCode != HttpURLConnection.HTTP_CREATED && res.statusCode != HttpURLConnection.HTTP_OK) { throw new NCMBException(res.mbStatus, res.mbErrorMessage); } return res; } catch (InterruptedException | ExecutionException | TimeoutException e) { throw new NCMBException(e); } } NCMBConnection(NCMBRequest request); void setCallbackListener(RequestApiCallback callback); NCMBResponse sendRequest(); void sendRequestAsynchronously(RequestApiCallback callback);  }