我试图获得“描述”#39;和'主要'使用预测openweathermap api从天气数组中的字符串。与天气api不同,预测有一系列的列表'在该阵列中,还有另一个叫做“天气”的阵列。我似乎无法将这些信息作为单个字符串获取。我希望能够展示“云”。和破碎的云'在TextView中
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();
}
}
}
}
答案 0 :(得分:0)
尝试直接使用getJsonArray获取&#34;列表&#34;值,然后您可以访问所需的索引,然后访问所需的键:
{'compound': 0.4404, 'neu': 0.58, 'pos': 0.42, 'neg': 0.0}
如果你想要它们,你可以把最后三行放在for循环中迭代以遍历所有数组。