0
我试图从天气阵列中使用预测的openweathermap API获取'描述'和'主'字符串。与天气api不同,预测有一个“列表”阵列,在该阵列内还有另一个名为“天气”的阵列。我似乎无法将这些信息视为单个字符串。我希望能够以一个TextView中显示“云”和“云彩”如何从openweathermap API的预测天气数组中获取描述字符串?
public class MainActivity extends AppCompatActivity {
EditText cityName;
TextView weatherTxt;
TextView nameText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
cityName = (EditText) findViewById(R.id.cityName);
weatherTxt = (TextView) findViewById(R.id.weatherTxt);
nameText = (TextView) findViewById(R.id.cityNameTxt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void GetWeather(View view){
Log.i ("city name", cityName.getText().toString());
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(cityName.getWindowToken(), 0);
downloadTask task = new downloadTask();
task.execute("http://api.openweathermap.org/data/2.5/forecast?q="
+cityName.getText().toString()+
"&appid=71aefce4499fe64969286582c7e80ea2");
}
public class downloadTask extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
InputStream in = urlConnection.getInputStream();
InputStreamReader reader = new InputStreamReader(in);
int data = reader.read();
while (data != -1){
char current = (char) data;
result += current;
data = reader.read();
}
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
String mainTxt = "";
String descriptionTxt = "";
String weather = "";
String tempratureTxt = "";
String main = "";
JSONObject jsonObject = new JSONObject(result);
JSONObject city = jsonObject.getJSONObject("city");
String name = city.getString("name");
Log.i("name", name);
JSONArray list = jsonObject.getJSONArray("list");
JSONObject listObject = list.getJSONObject(0);
for (int i =0; i <= 6; i++){ //look through all the list until you reach 7 (i.e. 7 day forecast)
JSONObject weatherInfo = listObject.getJSONArray("weather").getJSONObject(0);
String weatherMain = weatherInfo.getString("main");
String weatherDescription = weatherInfo.getString("description");
JSONObject mainTemp = listObject.getJSONObject("main");
String temp = mainTemp.getString("temp");
Log.i ("main", weatherMain);
Log.i ("description", weatherDescription);
Log.i ("temp", temp);
weather = weatherMain;
main = weatherDescription;
tempratureTxt = temp;
if (weather != ""){
mainTxt += "Day " +i+ ": " +weather + "\r\n";
descriptionTxt += main + "\r\n";
tempratureTxt += temp + "\r\n";
//Toast.makeText(getBaseContext(), message + " - "
// + message,Toast.LENGTH_SHORT).show();
}
}
//move this inside the while loop if you want to display all week
//This displays the weather information in the Activity TextView
if (mainTxt != ""){
weatherTxt.setText(mainTxt);
descriptionText.setText(descriptionTxt);
temperatureText.setText(tempratureTxt);
nameText.setText("The Weather in " +name+ " is: ");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
太棒了!已经工作。 唯一的问题是,看起来不行。 我已经更新了我的代码。有什么建议么? – Toby
没关系。我刚刚移动'JSONObject listObject = list.getJSONObject(0);'进入for循环。 – Toby