想要請問:
class TransTask extends AsyncTask<String, Void, String> implements OnMapReadyCallback {
StringBuffer json = new StringBuffer();
String Px;
String Py;
String sna;
JSONArray contacts;
private ArrayList movieList = new ArrayList<>();
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream is = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
while (line != null) {
json.append(line);
line = br.readLine();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return json.toString();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
JSONObject jonObject = null;
ArrayList<String> arrayList = new ArrayList<String>();
User = (hotelmovie) getApplicationContext();
try {
jonObject = new JSONObject(s.toString());
contacts = jonObject.getJSONObject("XML_Head").getJSONObject("Infos").getJSONArray("Info");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
sna = c.getString("Name");
Px = c.getString("Px");
Py = c.getString("Py");
hotelmovie movie = new hotelmovie(sna,Px,Py);
movieList.add(movie);
arrayList.add(sna);
User.setTitle(sna);
User.setPx(Px);
User.setPy(Py);
// User.setTitle(sna);
Log.i("Volley", User.getTitle());
}
} catch (JSONException e) {
e.printStackTrace();
}
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
for (int i = 0; i < contacts.length(); i++) {
Log.i("Volley 1", User.getTitle());
}
hotelmovie gv = (hotelmovie) getApplicationContext();
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
}
movieList, arrayList 都是 ArrayList
但是 User 就只是一個
class
for (int i = 0; i < contacts.length(); i++) {
Log.i("Volley 1", User.getTitle());
}
改成
for (int i = 0; i < contacts.length(); i++) {
Log.i("Volley 1", arrayList.get(i));
}
看看
好厲害~ 確實這樣子名字欄位全部都有讀到,但是,不好意思,但我想要再問一下,movieList 我原本是想要用全域變數去讀,那為什麼如果我想要改成用 movieList會沒辦法讀呢? 全域變數該設定的我都有做,不明白原因是什麼呢? 謝謝
public class hotelmovie extends Application {
private String title,px,py;
public hotelmovie() {
}
@Override
public void onCreate() {
super.onCreate();
setTitle(title); //初始化全域性變數
}
public hotelmovie(String title, String px, String py) {
this.title = title;
this.px = px;
this.py = py;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPx() {
return px;
}
public void setPx(String px) {
this.px = px;
}
public String getPy() {
return py;
}
public void setPy(String py) {
this.py = py;
}
private static final String NAME = "hotelmovie";
}