You are on page 1of 5

package com.example.goett.

myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
Timer timer = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView cotacao = (TextView) findViewById(R.id.cotacaocompra);
apiInterface = ServiceGenerator.getClient().create(APIInterface.class);
long TEMPO = (1000 * 1); // chama o método a cada 3 segundos
if (timer == null) {
timer = new Timer();
TimerTask tarefa = new TimerTask() {
public void run() {
try {
Call<Planetas> call = apiInterface.doGetListResources();
call.enqueue(new Callback<Planetas>() {
@Override
public void onResponse(Call<Planetas> call,
Response<Planetas> response) {
Log.d("TAGmamaria",response.code()+"");
String displayResponse = "";
Planetas resource = response.body();
String nome = resource.getName();
displayResponse= nome;
Log.d("TAG",displayResponse+"");
cotacao.setText(displayResponse);
}
@Override
public void onFailure(Call<Planetas> call, Throwable
t) {
call.cancel();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
};
timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);
}
}
}
package com.example.goett.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
Timer timer = null;
ArrayList<Planetas> planetList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView cotacao = (TextView) findViewById(R.id.cotacaocompra);
apiInterface = ServiceGenerator.getClient().create(APIInterface.class);
try {
Call<PlanetasLista> call = apiInterface.doGetListResources();
call.enqueue(new Callback<PlanetasLista>() {
@Override
public void onResponse(Call<PlanetasLista> call,
Response<PlanetasLista> response) {
if (response.isSuccessful()) {
Log.d("DataCheck",new
Gson().toJson(response.body()));
/**
* Got Successfully
*/
Log.d("teste",response.body().toString());
planetList = response.body().getPlaneta();
}
}
@Override
public void onFailure(Call<PlanetasLista> call, Throwable t)
{
call.cancel();
}
});
} catch (Exception e) {
e.printStackTrace();
}
if (planetList!=null) {
Log.d("teste", String.valueOf(planetList.get(0).getName()));
}
}
}

package com.example.goett.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Timer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class MainActivity extends AppCompatActivity {
APIInterface apiInterface;
Timer timer = null;
ArrayList<Planetas> planetList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView cotacao = (TextView) findViewById(R.id.cotacaocompra);
apiInterface = ServiceGenerator.getClient().create(APIInterface.class);
planetList = new ArrayList<Planetas>();
try {
Call<PlanetaResults> call = apiInterface.doGetListResources(1);
call.enqueue(new Callback<PlanetaResults>() {
@Override
public void onResponse(Call<PlanetaResults> call,
Response<PlanetaResults> response) {
if (response.isSuccessful()) {
Log.d("DataCheck",new
Gson().toJson(response.body().getResults()));
String jsonObj;
jsonObj = new
Gson().toJson(response.body().getResults());
try {
JSONArray jsonArr = new JSONArray(jsonObj);
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObjloop =
jsonArr.getJSONObject(i);
Planetas planetaAux = new Planetas();

planetaAux.setName(jsonObjloop.getString("name"));

planetaAux.setDiameter(jsonObjloop.getString("diameter"));

planetaAux.setRotation_period(jsonObjloop.getString("rotation_period"));
planetaAux.setOrbital_period(jsonObjloop.getString("orbital_period"));

planetaAux.setGravity(jsonObjloop.getString("gravity"));

planetaAux.setPopulation(jsonObjloop.getString("population"));

planetaAux.setClimate(jsonObjloop.getString("climate"));

planetaAux.setTerrain(jsonObjloop.getString("terrain"));

planetaAux.setSurface_water(jsonObjloop.getString("surface_water"));

planetaAux.setResidents(Collections.singletonList(jsonObjloop.getString("residen
ts")));

planetaAux.setFilms(Collections.singletonList(jsonObjloop.getString("films")));

planetaAux.setUrl(jsonObjloop.getString("url"));

planetaAux.setCreated(jsonObjloop.getString("created"));

planetaAux.setEdited(jsonObjloop.getString("edited"));
planetList.add(planetaAux);
//Log.d("teste",i+"
"+jsonObjloop.toString());
}
if (planetList!=null) {
if(planetList.size()>0){
for (int i = 0; i<planetList.size(); i+
+){
Log.d("teste2",i+1+"
"+planetList.get(i).getName());
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("teste",jsonObj);
// planetList.add( gson.fromJson(new
Gson().toJson(response.body().getResults()), Planetas.class));
}
else{
System.out.println("ERROR "+response.raw().body());
}
}
@Override
public void onFailure(Call<PlanetaResults> call, Throwable
t) {
call.cancel();
}
});
} catch (Exception e) {
e.printStackTrace();
}
//Log.d("testefinal",planetList.get(0).getName().toString());
}
}

You might also like