2016-11-04 63 views
0

我正在使用使用天氣服務的android應用程序。我目前使用openweathermap.org的API,並且我注意到在響應中還有一個圖標標記,我想知道你怎樣才能讓這個圖像顯示出來。我試圖擺弄它,但我似乎無法弄清楚。OpenWeatherMap圖標下載android

這是我的主要活動:

public class MainActivity extends Activity { 

String description; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 
@Override 
protected void onStart() { 
    super.onStart(); 

    Double lat = 55.676098; 

    Double lon = 12.568337; 

    String apiKey = "70c5bf4e84a725a8aeb3dd8c7df4c254"; 
    String urlAPI = "http://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&APPID=" + apiKey; 
    String imgURL = "http://openweathermap.org/img/w/" + description + ".png"; 
    Weather weatherApi = new Weather(); 
    weatherApi.execute(urlAPI); 
    weatherApi.execute(imgURL); 


} 
public void goToShowFishActivity(View view) { 

    Intent intent = new Intent(this, ShowFishiesActivity.class); 
    startActivity(intent); 

} 

public void goToAddNewCatchActivity(View view) { 
    Intent intent = new Intent(this, AddCatch.class); 
    startActivity(intent); 
} 

public void goToLogin(View view) { 
    Intent intent = new Intent(this, CreateUserActivity.class); 
    startActivity(intent); 
} 

private class Weather extends ReadHttpTask{ 
    @Override 
    protected void onPostExecute(CharSequence charSequence){ 

     String text = charSequence.toString(); 
     Integer start = text.indexOf("icon\":\"") + "icon\":\"".length(); 
     Integer end = text.indexOf("\"}",start); 
     description = text.substring(start, end); 


     TextView weatherTry = (TextView) findViewById(R.id.weatherTry); 
     weatherTry.setText(description); 


    } 
    } 

} 

我ReadHttpTask類:

public class ReadHttpTask extends AsyncTask<String, Void, CharSequence> { 
@RequiresApi(api = Build.VERSION_CODES.N) 
@Override 
protected CharSequence doInBackground(String...urls) { 
    String urlString = urls[0]; 
    try{ 
     CharSequence result = HttpHelper.GetHttpResponse(urlString); 
     return result; 
    } 
    catch (IOException ex){ 
     cancel(true); 
     String errorMessage = ex.getMessage() + "\n" + urlString; 
     Log.e("Something went wrong", errorMessage); 
     return errorMessage; 
    } 
    } 
} 

而且我HttpHelper類:

public class HttpHelper { 

@RequiresApi(api = Build.VERSION_CODES.N) 
public static CharSequence GetHttpResponse(String urlString) throws IOException { 
    URL url = new URL(urlString); 
    URLConnection connection = url.openConnection(); 
    if (!(connection instanceof HttpURLConnection)) { 
     throw new IOException("Not an HTTP connection"); 
    } 

    HttpURLConnection httpConnection = (HttpURLConnection) connection; 
    int responseCode = httpConnection.getResponseCode(); 
    if (responseCode != HttpURLConnection.HTTP_OK) { 
     String responseMessage = httpConnection.getResponseMessage(); 
     throw new IOException("HTTP response code: " + responseCode + " " + responseMessage); 

    } 
    InputStream inputStream = httpConnection.getInputStream(); 
    BufferedReader reader = null; 
    try { 
     reader = new BufferedReader(new InputStreamReader(inputStream)); 
     String line; 
     StringBuilder sb = new StringBuilder(); 
     while (true) { 
      line = reader.readLine(); 
      if (line == null) break; 
      sb.append(line); 
     } 
     return sb; 
    } finally { 
     if (reader != null) reader.close(); 
    } 
    } 
} 

回答

1

你的JSON得到的圖標值,如,09d或50d是圖標代碼。要獲取圖標,您需要創建一個url,例如:http://openweathermap.org/img/w/09d.png

我建議您將圖標代碼存儲爲string並使用Picasso實際顯示圖標。

String icon = yourJsonObject.getString("icon"); 
String iconUrl = "http://openweathermap.org/img/w/" + icon + ".png"; 

Picasso.with(context).load(iconUrl).into(yourImageView); 
+0

謝謝! :DD這就像一個魅力! – kennyYice23

+0

thnxxx ........救了我的一天 –