Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SelectbyID extends AsyncTask<String,Void,String> {
- String link = "http://survivinginporto.atwebpages.com/SelectbyID.php";
- int id;
- String JSON_STRING;
- @Override
- protected String doInBackground(String... params) {
- String username = params[0];
- try {
- URL url = new URL(link);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
- String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- OS.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- StringBuilder stringBuilder = new StringBuilder();
- while ((JSON_STRING = bufferedReader.readLine()) != null) {
- stringBuilder.append(JSON_STRING + "n");
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return stringBuilder.toString().trim();
- // InputStream IS = httpURLConnection.getInputStream();
- // IS.close();
- // httpURLConnection.disconnect();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- } catch (ProtocolException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- link = "http://survivinginporto.atwebpages.com/SelectbyID.php";
- System.out.println("2");
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- System.out.println("progress update");
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String id2)
- {
- System.out.println("COMEÇA O POST");
- // int id = 0;
- System.out.println("3");
- try
- {
- JSONObject jsonObject2 = new JSONObject(id2);
- JSONArray jsonArray2 = jsonObject2.getJSONArray("id2");
- for(int i=0;i<jsonArray2.length();i++)
- {
- JSONObject JO2 = jsonArray2.getJSONObject(i);
- System.out.println("run for"); //DOENST SHOW !
- //get an output on the screen
- id=JO2.getInt("id");
- System.out.println("SELECTBYID:::::::::::::::::::::::::::::::::::NESTE MOMENTO O ID = " + id);
- }
- System.out.println("O ID DO USER É: " + id);
- // RetornaID(id);
- System.out.println("4");
- turnidtovalue(id);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- <?php
- $host = "";
- $user = "";
- $pass = "";
- $db = "";
- $con = mysqli_connect($host,$user,$pass,$db);
- if(!$con)
- {
- //die("Error ".mysqli_connect_error());
- }
- else
- {
- //echo "<h3>connection success</h3>";
- }
- //Recebendo a informação
- $username = $_POST["username"];
- $sql = "select id from androidotp where username='$username'";
- $id2 = mysqli_query($con,$sql);
- $response = array();
- while($row=mysqli_fetch_array($id2))
- {
- array_push($response,array('id'=>$row[0]));
- }
- echo json_encode(array('id2'=>$response));
- mysqli_close($con);?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement