Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. package com.goosipe.girl;
  2.  
  3. import android.os.AsyncTask;
  4. import android.util.Log;
  5.  
  6. import org.json.JSONObject;
  7.  
  8. import java.io.BufferedInputStream;
  9. import java.io.BufferedReader;
  10. import java.io.IOException;
  11. import java.io.InputStream;
  12. import java.io.InputStreamReader;
  13. import java.net.HttpURLConnection;
  14. import java.net.MalformedURLException;
  15. import java.net.ProtocolException;
  16. import java.net.URL;
  17.  
  18.  
  19. public class MonJson extends AsyncTask {
  20.  
  21.  
  22. private boolean turnon = true;
  23. private String banner = "";
  24. private String interstitial = "";
  25. private String nativeads = "";
  26. private String startapp = "";
  27. private String store = "";
  28. private String usus = "";
  29. private String update = "";
  30.  
  31.  
  32. @Override
  33. protected Object doInBackground(Object... params) {
  34.  
  35.  
  36. this.parse();
  37. return this;
  38.  
  39.  
  40. }
  41.  
  42. private void parse()
  43. {
  44.  
  45. Gethttp sh = new Gethttp();
  46. String url = "https://api.myjson.com/bins/60ui0";
  47. String jsonStr = sh.makeServiceCall(url);
  48.  
  49. try {
  50. JSONObject jsonRoot = new JSONObject(jsonStr);
  51.  
  52.  
  53.  
  54. String turnonString = jsonRoot.getString("turnon");
  55. Log.d("turnonjson : ", turnonString);
  56. if(turnonString.equals("ON"))
  57. {
  58. turnon = true;
  59. }
  60. if(turnonString.equals("OFF"))
  61. {
  62. turnon = false;
  63. }
  64. store = jsonRoot.getString("store");
  65. banner = jsonRoot.getString("banner");
  66. interstitial = jsonRoot.getString("interstitial");
  67. startapp = jsonRoot.getString("startapp");
  68. nativeads = jsonRoot.getString("native");
  69. update = jsonRoot.getString("update");
  70. usus = jsonRoot.getString("usus");
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. }
  78. catch(Exception e)
  79. {
  80.  
  81. }
  82.  
  83. }
  84.  
  85.  
  86.  
  87. public String isUsUs()
  88. {
  89. return usus;
  90. }
  91. public String isUpdate()
  92. {
  93. return update;
  94. }
  95. public String isBanner()
  96. {
  97. return banner;
  98. }
  99.  
  100. public String isInterstitial()
  101. {
  102. return interstitial;
  103. }
  104.  
  105. public String isStartapp()
  106. {
  107. return startapp;
  108. }
  109.  
  110. public String isNative()
  111. {
  112. return nativeads;
  113. }
  114.  
  115. public boolean isTurnon()
  116. {
  117. return turnon;
  118. }
  119.  
  120. public String isStore()
  121. {
  122. return store;
  123. }
  124.  
  125. public class Gethttp {
  126.  
  127. private final String TAG = Gethttp.class.getSimpleName();
  128.  
  129. public Gethttp() {
  130. }
  131.  
  132. public String makeServiceCall(String reqUrl) {
  133. String response = null;
  134. try {
  135. URL url = new URL(reqUrl);
  136. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  137. conn.setRequestMethod("GET");
  138. // read the response
  139. InputStream in = new BufferedInputStream(conn.getInputStream());
  140. response = convertStreamToString(in);
  141. } catch (MalformedURLException e) {
  142. Log.e(TAG, "MalformedURLException: " + e.getMessage());
  143. } catch (ProtocolException e) {
  144. Log.e(TAG, "ProtocolException: " + e.getMessage());
  145. } catch (IOException e) {
  146. Log.e(TAG, "IOException: " + e.getMessage());
  147. } catch (Exception e) {
  148. Log.e(TAG, "Exception: " + e.getMessage());
  149. }
  150. return response;
  151. }
  152.  
  153. private String convertStreamToString(InputStream is) {
  154. BufferedReader reader = new BufferedReader(new InputStreamReader(is));
  155. StringBuilder sb = new StringBuilder();
  156.  
  157. String line;
  158. try {
  159. while ((line = reader.readLine()) != null) {
  160. sb.append(line).append('\n');
  161. }
  162. } catch (IOException e) {
  163. e.printStackTrace();
  164. } finally {
  165. try {
  166. is.close();
  167. } catch (IOException e) {
  168. e.printStackTrace();
  169. }
  170. }
  171.  
  172. return sb.toString();
  173. }
  174. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement