Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. public class SelectbyID extends AsyncTask<String,Void,String> {
  2. String link = "http://survivinginporto.atwebpages.com/SelectbyID.php";
  3. int id;
  4. String JSON_STRING;
  5.  
  6. @Override
  7. protected String doInBackground(String... params) {
  8. String username = params[0];
  9.  
  10. try {
  11.  
  12. URL url = new URL(link);
  13. HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
  14. httpURLConnection.setRequestMethod("POST");
  15. httpURLConnection.setDoOutput(true);
  16.  
  17.  
  18. OutputStream OS = httpURLConnection.getOutputStream();
  19. BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
  20. String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
  21.  
  22.  
  23.  
  24. bufferedWriter.write(data);
  25. bufferedWriter.flush();
  26. bufferedWriter.close();
  27. OS.close();
  28.  
  29.  
  30. InputStream inputStream = httpURLConnection.getInputStream();
  31. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  32. StringBuilder stringBuilder = new StringBuilder();
  33. while ((JSON_STRING = bufferedReader.readLine()) != null) {
  34. stringBuilder.append(JSON_STRING + "n");
  35. }
  36.  
  37. bufferedReader.close();
  38. inputStream.close();
  39. httpURLConnection.disconnect();
  40. return stringBuilder.toString().trim();
  41. // InputStream IS = httpURLConnection.getInputStream();
  42. // IS.close();
  43.  
  44. // httpURLConnection.disconnect();
  45.  
  46.  
  47. } catch (MalformedURLException e) {
  48. e.printStackTrace();
  49. } catch (UnsupportedEncodingException e) {
  50. e.printStackTrace();
  51. } catch (ProtocolException e) {
  52. e.printStackTrace();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56. return null;
  57. }
  58.  
  59.  
  60. @Override
  61. protected void onPreExecute() {
  62. link = "http://survivinginporto.atwebpages.com/SelectbyID.php";
  63. System.out.println("2");
  64. }
  65.  
  66. @Override
  67. protected void onProgressUpdate(Void... values) {
  68.  
  69. System.out.println("progress update");
  70. super.onProgressUpdate(values);
  71. }
  72.  
  73.  
  74. @Override
  75. protected void onPostExecute(String id2)
  76. {
  77. System.out.println("COMEÇA O POST");
  78. // int id = 0;
  79.  
  80.  
  81. System.out.println("3");
  82. try
  83. {
  84. JSONObject jsonObject2 = new JSONObject(id2);
  85. JSONArray jsonArray2 = jsonObject2.getJSONArray("id2");
  86.  
  87.  
  88. for(int i=0;i<jsonArray2.length();i++)
  89. {
  90.  
  91. JSONObject JO2 = jsonArray2.getJSONObject(i);
  92. System.out.println("run for"); //DOENST SHOW !
  93.  
  94. //get an output on the screen
  95.  
  96.  
  97.  
  98.  
  99. id=JO2.getInt("id");
  100. System.out.println("SELECTBYID:::::::::::::::::::::::::::::::::::NESTE MOMENTO O ID = " + id);
  101.  
  102.  
  103.  
  104. }
  105.  
  106.  
  107.  
  108. System.out.println("O ID DO USER É: " + id);
  109. // RetornaID(id);
  110. System.out.println("4");
  111. turnidtovalue(id);
  112.  
  113. } catch (JSONException e) {
  114. e.printStackTrace();
  115. }
  116.  
  117.  
  118. }
  119.  
  120. <?php
  121. $host = "";
  122. $user = "";
  123. $pass = "";
  124. $db = "";
  125.  
  126.  
  127. $con = mysqli_connect($host,$user,$pass,$db);
  128.  
  129. if(!$con)
  130. {
  131. //die("Error ".mysqli_connect_error());
  132. }
  133. else
  134. {
  135.  
  136. //echo "<h3>connection success</h3>";
  137.  
  138. }
  139.  
  140. //Recebendo a informação
  141. $username = $_POST["username"];
  142.  
  143.  
  144. $sql = "select id from androidotp where username='$username'";
  145.  
  146. $id2 = mysqli_query($con,$sql);
  147.  
  148. $response = array();
  149.  
  150.  
  151. while($row=mysqli_fetch_array($id2))
  152. {
  153.  
  154. array_push($response,array('id'=>$row[0]));
  155.  
  156.  
  157. }
  158.  
  159. echo json_encode(array('id2'=>$response));
  160.  
  161. mysqli_close($con);?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement