公共类FindPlaces扩展了AppCompatActivity {
private static final String MAP_API_URL = "https://api.foursquare.com/v2/venues/categories?client_id=AW31XQWUOLJWFZCFNZZ04I4IQFUFYIFAFDEHEYITEBBDHIVB&client_secret=R10S3ACA5VFOTGVG2LW1N4YONU30LQ3IE0DKIG1JNHN2QNWE&v=20170811&m=foursquare";
JSONObject jsonobject;
JSONArray jsonarray;
ProgressDialog mProgressDialog;
ArrayList<String> categoriesList;
ArrayList<Categories> categories;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find_places);
new DownloadJSON().execute();
}
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
// Locate the Categories Class
categories = new ArrayList<Categories>();
// Create an array to populate the spinner
categoriesList = new ArrayList<String>();
// JSON file URL address
jsonobject = JSONfunctions
.getJSONfromURL("https://api.foursquare.com/v2/venues/categories?client_id=CONSUMER_KEY&client_secret=CONSUMER_SECRET&v=20170811&m=foursquare");
try {
// Locate the NodeList name
jsonarray = jsonobject.getJSONArray("categories");
for (int i = 0; i < jsonarray.length(); i++) {
jsonobject = jsonarray.getJSONObject(i);
Categories cat = new Categories();
cat.setName(jsonobject.optString("name"));
categories.add(cat);
// Populate spinner with country names
categoriesList.add(jsonobject.optString("name"));
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the spinner in activity_main.xml
Spinner mySpinner = (Spinner) findViewById(R.id.spinnerCategories);
// Spinner adapter
mySpinner
.setAdapter(new ArrayAdapter<String>(FindPlaces.this,
android.R.layout.simple_spinner_dropdown_item,
categoriesList));
// Spinner on item click listener
mySpinner
.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0,
View arg1, int position, long arg3) {
// TODO Auto-generated method stub
// Locate the textviews in activity_main.xml
TextView txtname = (TextView) findViewById(R.id.tvName);
txtname.setText(categories.get(position).getName());
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
这是我的课程,给旋转器充气。
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url) {
InputStream is = null;
String result = "";
JSONObject jArray = null;
// Download JSON data from URL
try {
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
// Convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
Log.e("log_tag", "Error converting result " + e.toString());
}
try {
jArray = new JSONObject(result);
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
return jArray;
}
}
这是我处理http请求的地方
{“response”:{ “类别”:[ { “id”:“4d4b7104d754a06370d81259”, “名称”:“艺术与娱乐”, “pluralName”:“艺术与娱乐”, “shortName”:“艺术与娱乐”, “icon”:{ “prefix”:“https://ss3.4sqi.net/img/categories_v2/arts_entertainment/default_”, “后缀”:“。png” }, “类别”:[ { “id”:“56aa371be4b08b9a8d5734db”, “名字”:“露天剧场”, “pluralName”:“Amphitheatre”, “shortName”:“露天剧场”, “icon”:{ “prefix”:“https://ss3.4sqi.net/img/categories_v2/arts_entertainment/default_”, “后缀”:“。png” }
这是json。
如何更改获取请求而不是发布请求? 我怎么能填充我的微调器? 提前谢谢。