구글 날씨 API를 이용해서 날씨를 출력하는 앱을 만들고 있습니다.
웹상에서 구글API를 이용한 JSP소스를 구해서 그걸 이용해서 하려고 합니다.
JSP 소스 내용을 그냥 자바파일로 출력하면 잘 나오는데요
이상하게 저장된 내용을 안드로이드에서 setText로 뿌려주는 부분에서 출력이 안되네요..(굵게 하이라이트 된 부분입니다.)
또한가지 궁금한 점은
구글 API에 있는 내용을 보니 아이콘부분은 URL로만 나와있더군요
그래서 그 URL을 ImageView의 setImageURI로 출력해주려고 했는데
저렇게 하는게 맞나요??(빨간색으로 하이라이트 된 부분입니다)
package project.main;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
public class Weather extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weather);
TextView xDate = (TextView)findViewById(R.id.date);
ImageView xWeatherIcon = (ImageView)findViewById(R.id.weathericon);
TextView xNowCondition = (TextView)findViewById(R.id.nowCondition);
TextView xTempC = (TextView)findViewById(R.id.tempC);
TextView xTempF = (TextView)findViewById(R.id.tempF);
TextView xHumidity = (TextView)findViewById(R.id.humidity);
TextView xWindy = (TextView)findViewById(R.id.windy);
TextView xDay1 = (TextView)findViewById(R.id.day1);
ImageView xWeekicon1 = (ImageView)findViewById(R.id.weekicon1);
TextView xCondition1 = (TextView)findViewById(R.id.Condition1);
TextView xLowT1 = (TextView)findViewById(R.id.lowT1);
TextView xHigh1 = (TextView)findViewById(R.id.HighT1);
TextView xDay2 = (TextView)findViewById(R.id.day2);
ImageView xWeekicon2 = (ImageView)findViewById(R.id.weekicon2);
TextView xCondition2 = (TextView)findViewById(R.id.Condition2);
TextView xLowT2 = (TextView)findViewById(R.id.lowT2);
TextView xHigh2 = (TextView)findViewById(R.id.HighT2);
TextView xDay3 = (TextView)findViewById(R.id.day3);
ImageView xWeekicon3 = (ImageView)findViewById(R.id.weekicon3);
TextView xCondition3 = (TextView)findViewById(R.id.Condition3);
TextView xLowT3 = (TextView)findViewById(R.id.lowT3);
TextView xHigh3 = (TextView)findViewById(R.id.HighT3);
TextView xDay4 = (TextView)findViewById(R.id.day4);
ImageView xWeekicon4 = (ImageView)findViewById(R.id.weekicon4);
TextView xCondition4 = (TextView)findViewById(R.id.Condition4);
TextView xLowT4 = (TextView)findViewById(R.id.lowT4);
TextView xHigh4 = (TextView)findViewById(R.id.HighT4);
String XMLPathURL = "http://www.google.co.kr/ig/api?weather=seoul&ie=utf-8&oe=utf-8&hl=ko";
ArrayList weatherArray = new ArrayList();
String weatherCurrent[] = new String[6];
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
factory.setIgnoringElementContentWhitespace(true);
Document path = builder.parse(XMLPathURL);
Element root = path.getDocumentElement();
Element weather = (Element)root.getFirstChild();
boolean isForecast = false;
boolean isCurrent = false;
while ( weather != null){
Element xmlTagName = (Element)weather.getFirstChild();
while (xmlTagName != null){
Element childTagName = (Element) xmlTagName.getFirstChild();
Forecast_conditions forecast_conditions = null;
if( "forecast_conditions".equals(xmlTagName.getTagName().toString().trim())){
isForecast = true;
forecast_conditions = new Forecast_conditions();
}
else
isForecast = false;
if( "current_conditions".equals(xmlTagName.getTagName().toString().trim()))
isCurrent = true;
else
isCurrent = false;
while ( childTagName != null ){
String tagName = childTagName.getTagName();
String tagValue = childTagName.getAttribute("data");
if(isForecast){
if ( "day_of_week".equals(tagName))
forecast_conditions.setDay_of_week(tagValue);
else if ("low".equals(tagName))
forecast_conditions.setLow(Integer.parseInt(tagValue));
else if ("high".equals(tagName))
forecast_conditions.setHigh(Integer.parseInt(tagValue));
else if ("icon".equals(tagName))
forecast_conditions.setIcon(tagValue);
else if ("condition".equals(tagName))
forecast_conditions.setCondition(tagValue);
}
if(isCurrent){
if ("condition".equals(tagName))
weatherCurrent[0] = tagValue;
if ("temp_f".equals(tagName))
weatherCurrent[1] = tagValue;
if ("temp_c".equals(tagName))
weatherCurrent[2] = tagValue;
if ("humidity".equals(tagName))
weatherCurrent[3] = tagValue;
if ("icon".equals(tagName))
weatherCurrent[4] = tagValue;
if ("wind_condition".equals(tagName))
weatherCurrent[5] = tagValue;
}
childTagName = (Element) childTagName.getNextSibling();
}
if(isForecast)
weatherArray.add(forecast_conditions);
xmlTagName = (Element) xmlTagName.getNextSibling();
}
weather = (Element) weather.getNextSibling();
}
}
catch (Exception e){ System.out.println(e); }
xDate.setText("왜안나와");
xNowCondition.setText(weatherCurrent[0]);
xTempC.setText(weatherCurrent[2]);
xTempF.setText(weatherCurrent[1]);
xHumidity.setText(weatherCurrent[3]);
weatherCurrent[4] = "http://www.google.co.kr" + weatherCurrent[4];
xWeatherIcon.setImageURI(Uri.parse(weatherCurrent[4]));
xWindy.setText(weatherCurrent[5]);
for ( int x = 0 ; x < weatherArray.size(); x++) {
Forecast_conditions forecast_conditions = (Forecast_conditions) weatherArray.get(x);
String iconURL = null;
if(x == 0){
xDay1.setText(forecast_conditions.getDay_of_week() + "요일");
xCondition1.setText(forecast_conditions.getCondtion());
xHigh1.setText(forecast_conditions.getHigh() + "°");
xLowT1.setText(forecast_conditions.getLow() + "°/");
iconURL = "http://www.google.co.kr" + forecast_conditions.getIcon();
xWeekicon1.setImageURI(Uri.parse(iconURL));
}
else if(x == 1){
xDay2.setText(forecast_conditions.getDay_of_week() + "요일");
xCondition2.setText(forecast_conditions.getCondtion());
xHigh2.setText(forecast_conditions.getHigh() + "°");
xLowT2.setText(forecast_conditions.getLow() + "°/");
iconURL = "http://www.google.co.kr" + forecast_conditions.getIcon();
xWeekicon2.setImageURI(Uri.parse(iconURL));
}
else if(x == 2){
xDay3.setText(forecast_conditions.getDay_of_week() + "요일");
xCondition3.setText(forecast_conditions.getCondtion());
xHigh3.setText(forecast_conditions.getHigh() + "°");
xLowT3.setText(forecast_conditions.getLow() + "°/");
iconURL = "http://www.google.co.kr" + forecast_conditions.getIcon();
xWeekicon3.setImageURI(Uri.parse(iconURL));
}
else if(x == 3){
xDay4.setText(forecast_conditions.getDay_of_week() + "요일");
xCondition4.setText(forecast_conditions.getCondtion());
xHigh4.setText(forecast_conditions.getHigh() + "°");
xLowT4.setText(forecast_conditions.getLow() + "°/");
iconURL = "http://www.google.co.kr" + forecast_conditions.getIcon();
xWeekicon4.setImageURI(Uri.parse(iconURL));
}
}
}
public class Forecast_conditions {
private String day_of_week, icon, condition;
private int low, high;
public void setDay_of_week(String day_of_week){
this.day_of_week = day_of_week;
}
public String getDay_of_week(){
return this.day_of_week;
}
public void setIcon(String icon){
this.icon = icon;
}
public String getIcon(){
return this.icon ;
}
public void setCondition(String condition){
this.condition = condition;
}
public String getCondtion(){
return this.condition;
}
public void setLow(int low){
this.low = low;
}
public int getLow(){
return this.low;
}
public void setHigh(int high){
this.high = high;
}
public int getHigh(){
return this.high;
}
}
}



