Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. kelas klik intent untuk pindah ke layaout kuis (di json saya seperti ini
  2. $id_level = $_GET['id_level'];
  3. $mysql = mysqli_query($con, "SELECT tbl_quiz.*, tbl_level.level FROM tbl_quiz, tbl_level where tbl_quiz.id_level = tbl_level.id_level and tbl_level.id_level = '$id_level'"
  4. )
  5. @Override
  6. public void onClick(View v) {
  7. new FetchQuestionsAsync(this).execute(DATA_URL + "?id_level=1234");
  8. if(pass.size()!=0) {
  9. Intent intent = new Intent(context, list_quiz.class);
  10. intent.putExtra(DATA, pass);
  11. for (Question q: pass) {
  12. Toast.makeText(context, q.getQuestion(), Toast.LENGTH_LONG).show();
  13. System.out.println("isinya: " + q.getQuestion());
  14. }
  15. v.getContext().startActivity(intent);
  16. }
  17. else
  18. {
  19. Toast.makeText(context,"try Again",Toast.LENGTH_SHORT).show();
  20. }
  21. }
  22.  
  23. =====================================================================================
  24. package com.pengenalan.budaya.quiz;
  25.  
  26. import android.content.Context;
  27. import android.os.AsyncTask;
  28.  
  29.  
  30. import java.io.BufferedReader;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import java.net.HttpURLConnection;
  34. import java.util.ArrayList;
  35.  
  36.  
  37. public class FetchQuestionsAsync extends AsyncTask<String,Void,ArrayList<Question>>{
  38.  
  39. IData activity;
  40.  
  41. public FetchQuestionsAsync(IData activity) {
  42. this.activity = activity;
  43. }
  44.  
  45. @Override
  46. protected ArrayList<Question> doInBackground(String... params) {
  47. RequestParams requestParams=new RequestParams("GET",params[0]);
  48. StringBuilder stringBuilder=new StringBuilder("");
  49. try {
  50. HttpURLConnection httpURLConnection=requestParams.setUpConnection();
  51. httpURLConnection.connect();
  52.  
  53. int status_code = httpURLConnection.getResponseCode();
  54. if (status_code == HttpURLConnection.HTTP_OK) {
  55. BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
  56. stringBuilder = new StringBuilder("");
  57. String line = "";
  58. while ((line = br.readLine()) != null) {
  59. stringBuilder.append(line);
  60. }
  61. }
  62. } catch (IOException e) {
  63. e.printStackTrace();
  64. }
  65. ArrayList<Question> list=QuizUtil.parser(stringBuilder.toString());
  66.  
  67. return list;
  68. }
  69.  
  70. @Override
  71. protected void onPostExecute(ArrayList<Question> questions) {
  72. super.onPostExecute(questions);
  73. activity.setUpData(questions);
  74. }
  75.  
  76. static public interface IData
  77. {
  78. public void setUpData(ArrayList<Question> questions);
  79. public Context getContext();
  80. }
  81. }
  82.  
  83. ========================================================================================================
  84. package com.pengenalan.budaya.quiz;
  85.  
  86. import android.util.Log;
  87.  
  88. import java.io.IOException;
  89. import java.io.OutputStreamWriter;
  90. import java.io.UnsupportedEncodingException;
  91. import java.net.HttpURLConnection;
  92. import java.net.URL;
  93. import java.net.URLEncoder;
  94. import java.util.HashMap;
  95.  
  96. /**
  97. * Created by febin on 06/02/2017.
  98. */
  99.  
  100. public class RequestParams {
  101. String method;
  102.  
  103. public RequestParams(String method, String baseURL) {
  104. this.method = method;
  105. this.baseURL = baseURL;
  106. }
  107.  
  108. String baseURL;
  109. HashMap<String,String> params=new HashMap<>();
  110. public void addParam(String key,String value)
  111. {
  112. params.put(key,value);
  113. }
  114. public String getEncodedParams()
  115. {
  116. StringBuilder stringBuilder=new StringBuilder();
  117. for(String key:params.keySet())
  118. {
  119. try {
  120. String value= URLEncoder.encode(params.get(key),"UTF-8");
  121. if(stringBuilder.length()>0)
  122. {
  123. stringBuilder.append("&");
  124.  
  125. }
  126. stringBuilder.append(key+"="+value);
  127. } catch (UnsupportedEncodingException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. return stringBuilder.toString();
  132. }
  133. public String getEncodedURL()
  134. {
  135. return baseURL+"?"+getEncodedParams();
  136. }
  137. public HttpURLConnection setUpConnection() throws IOException {
  138. HttpURLConnection httpURLConnection=null;
  139. if(method.equals("GET"))
  140. {
  141. Log.d("check here","test");
  142. URL url=new URL(getEncodedURL());
  143. httpURLConnection=(HttpURLConnection)url.openConnection();
  144. httpURLConnection.setRequestMethod("GET");
  145. return httpURLConnection;
  146. }
  147. else
  148. {
  149. URL url=new URL(this.baseURL);
  150. httpURLConnection=(HttpURLConnection)url.openConnection();
  151. httpURLConnection.setRequestMethod("POST");
  152. httpURLConnection.setDoOutput(true);
  153. OutputStreamWriter outputStreamWriter=new OutputStreamWriter(httpURLConnection.getOutputStream());
  154. outputStreamWriter.write(getEncodedParams());
  155. outputStreamWriter.flush();
  156. }
  157. return httpURLConnection;
  158. }
  159.  
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement