Advertisement
Guest User

add comment

a guest
Jul 4th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. /**
  2.      * Passings the arguments, the functions will, assyncronously create a comment in database
  3.      * @param author id of author
  4.      * @param text text of comment
  5.      * @param date_hour date of cooment
  6.      * @param post id of post where the comment will be made
  7.      * @return return the comment created ou null if any error
  8.      * @throws IOException
  9.      * @throws ExecutionException
  10.      * @throws InterruptedException
  11.      */
  12.     public static Comment addComment(final long author, final String text, final String date_hour, final long post) throws IOException, ExecutionException, InterruptedException {
  13.         AsyncTask<Void, Void, Comment> task = new AsyncTask<Void, Void, Comment>() {
  14.             protected Comment doInBackground(Void... params) {
  15.                 URL url;
  16.                 Comment ret = null;
  17.                 try {
  18.                     url = new URL(RequestUtils.SERVERURL+ "/comment/add");
  19.                     HttpURLConnection con = (HttpURLConnection) url.openConnection();
  20.                     con.setDoOutput(true);
  21.                     con.setDoInput(true);
  22.                     con.setRequestMethod("POST");
  23.                     ByteArrayOutputStream baos = new ByteArrayOutputStream();
  24.                     //MUST INCLUDE HEADER
  25.                     baos.write(RequestUtils.header());
  26.  
  27.                     baos.write(RequestUtils.data(TEXT_COL_DB, text));
  28.                     baos.write(RequestUtils.data(DATE_HOUR_COL_DB, date_hour));
  29.                     baos.write(RequestUtils.data(AUTHOR_COL_DB, String.valueOf(author)));
  30.                     baos.write(RequestUtils.data(POST_COL_DB, String.valueOf(post)));
  31.  
  32.                     //MUST INCLUDE endRequest
  33.                     baos.write(RequestUtils.endRequest());
  34.  
  35.                     byte[] to_stream = baos.toByteArray();
  36.                     baos.close();
  37.                     con.setFixedLengthStreamingMode(to_stream.length);
  38.                     con.setRequestProperty("Connection", "Keep-Alive");
  39.                     con.setRequestProperty("User-Agent", "Android Multipart HTTP Client 1.0");
  40.                     con.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + RequestUtils.BOUNDARY);
  41.                     con.getOutputStream().write(to_stream);
  42.                     int code = con.getResponseCode();
  43.                     if(code == HttpURLConnection.HTTP_OK){
  44.                         String jsonString = RequestUtils.readStream(con.getInputStream());
  45.                         ret = Comment.getFromJson(jsonString);
  46.                     }else if(code == HttpURLConnection.HTTP_INTERNAL_ERROR){
  47.                         String out = RequestUtils.readStream(con.getErrorStream());
  48.                         Log.e("myDebug", out);
  49.                     }
  50.                     con.disconnect();
  51.  
  52.                 } catch (IOException e) {
  53.                     e.printStackTrace();
  54.                 } catch (ParseException e) {
  55.                     e.printStackTrace();
  56.                 } catch (JSONException e) {
  57.                     e.printStackTrace();
  58.                 }
  59.                 return ret;
  60.             }
  61.         };
  62.         task.execute();
  63.         return task.get();
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement