80,349
社区成员
发帖
与我相关
我的任务
分享
package com.secondwork.redrock.weatherapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.LinearLayout;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;
public class OutputActivity extends AppCompatActivity {
String CITY;
String Wendu;
String Ganmao;
WeatherAdapter adapter;
List<WeatherData> weatherDataList= new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_output);
Intent intent = getIntent();
CITY = intent.getStringExtra("city");//city通过另一活动得到
adapter = new WeatherAdapter(weatherDataList);
String get;
TextView temp = (TextView) findViewById(R.id.wendu);
TextView city = (TextView) findViewById(R.id.city);
TextView tips=(TextView)findViewById(R.id.ganmao);
initWeatherDatas();//处理数据
city.setText(CITY);//只显示city
tips.setText(Ganmao);//不显示
temp.setText(Wendu);//不显示
get= (String) tips.getText();//能得到值
RecyclerView recyclerView=findViewById(R.id.rv);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(adapter);
}
private void initWeatherDatas(){
HttpConnect httpConnect = new HttpConnect("https://www.apiopen.top/weatherApi?city="+CITY);
httpConnect.sendRequestWithHttpURLConnection(new HttpConnect.Callback() {
@Override
public void finish(String response) {
parseJson(response);
}
});
}
private void parseJson(String response) {
try {
JSONObject jsonObject = new JSONObject(response);
JSONObject data = new JSONObject(jsonObject.getString("data"));
JSONObject yesterday= new JSONObject(data.getString("yesterday"));
Ganmao=data.getString("ganmao");//所需字符串
Wendu = data.getString("wendu");//所需字符串
String date = yesterday.getString("date");
String high = yesterday.getString("high");
String fengxiang = yesterday.getString("fx");
String low = yesterday.getString("low");
String fl = yesterday.getString("fl");
String type = yesterday.getString("type");
WeatherData weatherData = new WeatherData(date,high,low,fl,fengxiang,type);
weatherDataList.add(weatherData);
JSONArray jsonArray = new JSONArray(data.getString("forecast"));
for(int i=0;i<jsonArray.length();i++){
JSONObject weather = jsonArray.getJSONObject(i);
date = weather.getString("date");
high = weather.getString("high");
fengxiang = weather.getString("fengxiang");
low = weather.getString("low");
fl = weather.getString("fengli");
type = weather.getString("type");
weatherData = new WeatherData(date,high,low,fl,fengxiang,type);
weatherDataList.add(weatherData);
}
runOnUiThread(new Runnable() {
@Override
public void run() {
adapter.notifyDataSetChanged();
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".OutputActivity"
tools:layout_editor_absoluteY="81dp">
<TextView
android:id="@+id/city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="48dp"
android:layout_marginEnd="8dp"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/wendu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textColor="#2e2d2d"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/city" />
<TextView
android:id="@+id/ganmao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textColor="#5e5959"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>