想要打印NSDictioanary,如key = value,key2 = value2,key3 = value3
是否可以采用内联方式? 如果值是字符串,则值应该有瑕疵。
答案 0 :(得分:0)
在斯威夫特:
@Override
public void onStart() {
super.onStart();
if(connectiondetector.isConnected()){
new jsontask().execute(
"https://newsapi.org/v1/articles?source=google-news&sortBy=top&apiKey=ade8f00a634b4825a028837ec107afae"
);
}else {
Toast.makeText(page.this, "no internet connection",Toast.LENGTH_SHORT).show();
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
}
@Override
public void onStop() {
super.onStop();
}
public class jsontask extends AsyncTask<String, String, List<moviemodel>> {
@Override
protected List<moviemodel> doInBackground(String... params) {
BufferedReader reader = null;
HttpURLConnection connection = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finaljson = buffer.toString();
JSONObject parentobject = new JSONObject(finaljson);
// JSONObject parentobject1 = parentobject.getJSONObject("");
JSONArray parentarray = parentobject.getJSONArray("articles");
List<moviemodel>moviemodelList = new ArrayList<>();
for (int i = 0; i < parentarray.length(); i++){
JSONObject finalobject = parentarray.getJSONObject(i);
moviemodel moviemodel = new moviemodel();
moviemodel.setAuthor(finalobject.getString("author"));
moviemodel.setDescription(finalobject.getString("description"));
moviemodel.setTitle(finalobject.getString("title"));
moviemodel.setImage(finalobject.getString("urlToImage"));
moviemodel.setUrl(finalobject.getString("url"));
moviemodel.setPublishedAt(finalobject.getString("publishedAt"));
moviemodelList.add(moviemodel);
}
return moviemodelList;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(List<moviemodel> result) {
super.onPostExecute(result);
newsadapter adapter = new newsadapter(getApplicationContext(),R.layout.row, result);
lvnews.setAdapter(adapter);
lvnews.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(firstVisibleItem+visibleItemCount == totalItemCount && totalItemCount!=0)
{
if(flag_loading == false)
{
flag_loading = true;
new jsontask().execute("https://newsapi.org/v1/articles?source=ars-technica&sortBy=top&apiKey=ade8f00a634b4825a028837ec107afae")
;}
}
}
});
}
}
在Objective-C中:
print(nameOfYourDictionary)