Skip to content

Instantly share code, notes, and snippets.

@miaodonghan
Created February 23, 2016 05:12
Show Gist options
  • Save miaodonghan/b88b6505c7dd1fbadfe1 to your computer and use it in GitHub Desktop.
Save miaodonghan/b88b6505c7dd1fbadfe1 to your computer and use it in GitHub Desktop.
@Override
protected String doInBackground(String... data) {
HttpURLConnection urlConnection = null;
try {
URL url = new URL("http://192.168.1.7:1337/api/doc");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestProperty("Content-type", "application/json");
urlConnection.setRequestProperty("charset", "utf-8");
JSONObject jsonParam = new JSONObject();
jsonParam.put("name", "25");
jsonParam.put("content", "Real");
String requestData = jsonParam.toString();
urlConnection.setRequestProperty("Content-Length", "" + requestData.getBytes().length);
DataOutputStream out = new DataOutputStream(urlConnection.getOutputStream());
out.writeBytes(requestData);
out.flush ();
out.close ();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
Scanner s = new Scanner(in).useDelimiter("\\A");
String res = s.hasNext() ? s.next() : "";
return res;
} catch (Exception ex) {
Log.e("er55r",ex.getMessage());
} finally {
urlConnection.disconnect();
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment