我试图从json获取数据。其实它正在工作,但我有一个listview.Im出现问题。数组列表和列表视图
下面是代码; MainActivity.java
public class MainActivity extends AppCompatActivity {
TextView txt;
Places places=new Places();
ListView listView;
List<Places> PlacesList=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt= (TextView) findViewById(R.id.text);
listView= (ListView) findViewById(R.id.listView);
listView.setBackgroundColor(Color.BLACK);
new getData().execute("https://api.foursquare.com/v2/venues/search?ll=41,29&oauth_token=01ZV1VDLE41USSRAYJUXOWNJ2MXYCAVN1I2PQFLFBCD40XYI&v=20160304");
}
public class getData extends AsyncTask<String,String,List <Places>> {
HttpURLConnection conn=null;
URL url=null;
BufferedReader bReader=null;
String name=null;
@Override
protected List<Places> doInBackground(String... params) {
try {
//BURADA URL'E BAGLANDIK
url=new URL(params[0]);
conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.connect();
//VERİ OKUMAK İÇİN STREAM KULLANACAZ
InputStream istream=conn.getInputStream();
bReader= new BufferedReader(new InputStreamReader(istream));
//VERİ DEPOLAMAK İÇİN StringBuffer
StringBuffer sBuffer=new StringBuffer();
String line="";
while((line=bReader.readLine())!=null){
sBuffer.append(line);
}
String JSONFinal=sBuffer.toString();
JSONObject jsonObject=new JSONObject(JSONFinal);
JSONArray jsonArray=jsonObject.getJSONObject("response").getJSONArray("venues");
StringBuffer finalBuffer=new StringBuffer();
for(int i=0;i<jsonArray.length();i++) {
JSONObject finalJsonObject = jsonArray.getJSONObject(i);
name = finalJsonObject.getString("name");
places.setName(name);
PlacesList.add(places);
}
return PlacesList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
finally {
conn.disconnect();
}
return null;
}
@Override
protected void onPostExecute(List<Places> PlacesList) {
super.onPostExecute(PlacesList);
ArrayAdapter adapter= new ArrayAdapter<Places>(getApplicationContext(),android.R.layout.simple_list_item_1,PlacesList);
listView.setAdapter(adapter);
}
}
Places.java
public class Places {
int id,lat,lng;
String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
那么是什么问题?
也发布你的json。 –
这是我的json http://www.jsoneditoronline.org/?url=https://api.foursquare.com/v2/venues/search?ll=41,29&oauth_token=01ZV1VDLE41USSRAYJUXOWNJ2MXYCAVN1I2PQFLFBCD40XYI&v=20160304 – Azat
我无法看到任何在你的json中称为name的字符串。 –