우선 코드를 먼저 보여드리겠습니다.
메인 액티비티 입니다.
public class MainActivity extends AppCompatActivity {
private Button btnShowLocation;
private TextView txtLat;
private TextView txtLon;
TextView tempa;
TextView mintemp;
TextView maxtemp;
String temp;
String temp_min;
String temp_max;
// GPSTracker class
private Gps gps;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowLocation = (Button) findViewById(R.id.btn_start);
txtLat = (TextView) findViewById(R.id.tv_latitude);
txtLon = (TextView) findViewById(R.id.tv_longitude);
// temp = (TextView)findViewById(R.id.temp);
// mintemp = (TextView)findViewById(R.id.mintemp);
// maxtemp = (TextView)findViewById(R.id.maxtemp);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// 권한 요청을 해야 함
if (!isPermission) {
callPermission();
return;
}
gps = new Gps(MainActivity.this);
// GPS 사용유무 가져오기
if (gps.isGetLocation()) {
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String temp = gps.temp;
String temp_min = gps.temp_min;
String temp_max = gps.temp_max;
txtLat.setText(String.valueOf(latitude));
txtLon.setText(String.valueOf(longitude));
밑에가 짤리긴 했지만 상관없는 내용이라서 지웠습니다.
그리고 Gps객체 소스 중 어싱크 부분입니다.입니다.(이것도 위에 그냥 필요없는건 빼고)
private class WeatherTask extends AsyncTask<String, Void, JSONObject>{
protected JSONObject doInBackground(String... datas) {
~~~~~~~~~~~~~~~~~~~~~~~~~
}
@Override
protected void onPostExecute(JSONObject result){
Log.i("TAG", result.toString());
if(result != null){
try{
temp = result.getJSONObject("main").getString("temp");
temp_min = result.getJSONObject("main").getString("temp_min");
temp_max = result.getJSONObject("main").getString("temp_max");
} catch (JSONException e){
e.printStackTrace();
}
}
}
}
일단 어싱크 태스크 execute 해서 돌아가는거 확인했고, onpost에 json으로 받아온걸
String타입의 temp, temp_min, temp_max 에 각각 집어넣었습니다.
그러면 위에 처음에 String으로 만들어놓은 temp, temp_min, temp_max에 저장될텐데
그게 main에서(빨갛게 칠해놓은 Gps객체) Gps객체 생성하고 gsp.temp 요런식으로 값 불러올려고 할때 자꾸
null값으로 떠서 답답합니다ㅠㅜㅜㅠㅜ 뭔가 아직 개념이 부족하고 지식이 없어서 그런데
어떻게 해야 Gps 객체에 어싱크 태스크가 실행됐을때 Onpost에 저장된 String 값이 메인에서도 나오게 할수 있나요?? 아직 초보라서 잘 모르겠습니다ㅠㅜㅠㅜㅠㅜㅠ(글자수 때문에 많이 이것저것 빼긴햇는데 보시는데는 크게 힘들거 같지 않을거 같아서 뺐습니다.....)