Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. package com.xyz.dbapp;
  2. import android.net.Uri;
  3. import android.util.Log;
  4. import org.json.JSONException;
  5. import org.json.JSONObject;
  6. import java.io.BufferedReader;
  7. import java.io.BufferedWriter;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.io.InputStreamReader;
  11. import java.io.OutputStream;
  12. import java.io.OutputStreamWriter;
  13. import java.io.UnsupportedEncodingException;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16.  
  17. /**
  18. * Created by selvakumar on 4/29/2015.
  19. */
  20. public class JSONParser
  21. {
  22. static JSONObject jObj = null;
  23. static String json = "";
  24. public static final int CONNECTION_TIMEOUT=10000;
  25. public static final int READ_TIMEOUT=15000;
  26. HttpURLConnection conn;
  27. URL url = null;
  28. public JSONParser() { }
  29. public JSONObject makeHttpRequest(String strurl,String... params)
  30. {
  31. try
  32. {
  33. url = new URL(strurl);
  34. conn = (HttpURLConnection)url.openConnection();
  35. conn.setReadTimeout(READ_TIMEOUT);
  36. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  37. conn.setRequestMethod("POST");
  38. conn.setDoInput(true);
  39. conn.setDoOutput(true);
  40. Uri.Builder builder = new Uri.Builder()
  41. .appendQueryParameter("username", params[0])
  42. .appendQueryParameter("password", params[1]);
  43. String query = builder.build().getEncodedQuery();
  44. OutputStream os = conn.getOutputStream();
  45. BufferedWriter writer = new BufferedWriter(
  46. new OutputStreamWriter(os, "UTF-8"));
  47. writer.write(query);
  48. writer.flush();
  49. writer.close();
  50. os.close();
  51. conn.connect();
  52. }
  53. catch (UnsupportedEncodingException e)
  54. {
  55. e.printStackTrace();
  56. }
  57. catch (IOException e)
  58. {
  59. e.printStackTrace();
  60. }
  61. try
  62. {
  63. int response_code = conn.getResponseCode();
  64. // Check if successful connection made
  65. if (response_code == HttpURLConnection.HTTP_OK)
  66. {
  67. // Read data sent from server
  68. InputStream input = conn.getInputStream();
  69. BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  70. StringBuilder result = new StringBuilder();
  71. String line;
  72. while ((line = reader.readLine()) != null)
  73. {
  74. result.append(line);
  75. }
  76. // Pass data to onPostExecute method
  77. //return(result.toString());
  78. json = result.toString();
  79. }
  80. }
  81. catch (Exception e)
  82. {
  83. Log.e("Buffer Error", "Error converting result " + e.toString());
  84. }
  85. // try parse the string to a JSON object
  86. try
  87. {
  88. jObj = new JSONObject(json);
  89. }
  90. catch (JSONException e)
  91. {
  92. Log.e("JSON Parser", "Error parsing data " + e.toString());
  93. }
  94. // return JSON String
  95. return jObj;
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement