daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest Jan 12th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MainActivity extends AppCompatActivity {
  2. //ATRIBUTOS
  3. private Button btOK;
  4. private TextView tvResposta;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8.     super.onCreate(savedInstanceState);
  9.     setContentView(R.layout.activity_main);
  10.  
  11.     btOK = (Button) findViewById(R.id.bt_consultar);
  12.     tvResposta = (TextView) findViewById(R.id.tv_resposta);
  13.  
  14.     btOK.setOnClickListener(new View.OnClickListener() {
  15.         @Override
  16.         public void onClick(View view) {
  17.             //FAZENDO A CONSULTA NO WEBSERVICES
  18.             try{
  19.                 Coins coins = new HTTPService().execute().get();
  20.                 tvResposta.setText(coins.toString()); //aqui chama a função to string
  21.  
  22.             } catch (InterruptedException e) {
  23.                 e.printStackTrace();
  24.             } catch (ExecutionException e) {
  25.                 e.printStackTrace();
  26.             }
  27.         }
  28.     });
  29. }
  30. }
  31.    
  32. public class Coins {
  33.  
  34. private String id;
  35. private String name;
  36. private String symbol;
  37. private String rank;
  38. private String price_usd;
  39. private String price_btc;
  40. //private String volume_usd_24h;
  41. private String market_cap_usd;
  42. private String available_supply;
  43. private String total_supply;
  44. private String percent_change_1h;
  45. private String percent_change_24h;
  46. private String percent_change_7d;
  47. private String last_updated;
  48.  
  49. public String converter(){
  50.     return "id: "+ getId()
  51.             +"n name: "+ getName()
  52.             +"n rank: "+ getRank()
  53.             +"n price_usd: "+ getPrice_usd()
  54.             +"n price_btc: "+getPrice_btc();
  55. }
  56.    
  57. public class HTTPService extends AsyncTask<Void, Void, Coins> {
  58.  
  59.  
  60. @Override
  61. protected Coins doInBackground(Void... voids) {
  62.     StringBuilder resposta = new StringBuilder();
  63.  
  64.     try{
  65.         //URL QUE SERÁ CONSUMIDA
  66.         URL url = new URL("https://api.coinmarketcap.com/v1/ticker/bitcoin/");
  67.  
  68.         //---- ABRINDO A CONEXÃO ---
  69.         HttpURLConnection conexao = (HttpURLConnection) url.openConnection();
  70.         conexao.setRequestMethod("GET");
  71.         conexao.setRequestProperty("Content-type","application/json");
  72.         conexao.setRequestProperty("Accept", "application/json");
  73.         conexao.setDoOutput(true);
  74.         conexao.setConnectTimeout(5000);
  75.         conexao.connect();
  76.  
  77.         //----- LENDO AS INFORMAÇÕES -------
  78.         Scanner scanner = new Scanner(url.openStream());
  79.  
  80.         while (scanner.hasNext()){
  81.             resposta.append(scanner.next());
  82.         }
  83.  
  84.     } catch (MalformedURLException e) {
  85.         e.printStackTrace();
  86.     } catch (IOException e) {
  87.         e.printStackTrace();
  88.     }
  89.     //---- convertendo dados do JSON
  90.     return new Gson().fromJson(resposta.toString(), Coins.class);
  91. }
  92.  
  93. @Override
  94. protected void onPostExecute(Coins coins) {
  95.     super.onPostExecute(coins);
  96. }
  97.  
  98. @Override
  99. protected void onProgressUpdate(Void... values) {
  100.     super.onProgressUpdate(values);
  101. }
  102. }
  103.    
  104. return new Gson().fromJson(resposta.toString(), Coins.class);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top