问题描述
我在以下链接
我正在尝试将其转换为JAVA对象,但无法加载图像.
I am trying to convert it to JAVA Object but i failed to load the IMAGE.
这是我的代码
public class MainActivity extends ActionBarActivity {
// Declare Variables
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String url = "https://www.googleapis.com/youtube/v3/search?part=snippet&maxResult=30&q=natok+bangla+mosharrof+karim&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA";
static String VIDEO_ID = "videoId";
static String TITLE = "title";
//static String DESCRIPTION = "description";
static String THUMBNAILS = "http://img.youtube.com/vi/\" + videoId + \"/hqdefault.jpg\"";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from listview_main.xml
setContentView(R.layout.listview_main);
// Execute DownloadJSON AsyncTask
new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
mProgressDialog = new ProgressDialog(MainActivity.this);
// Set progressdialog title
mProgressDialog.setTitle("Your Youtube Video is");
// Set progressdialog message
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
// Show progressdialog
mProgressDialog.show();
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
String query = "bangla natok 2015";
query = query.replace(" ", "+");
try {
query = URLEncoder.encode(query, "utf-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
jsonobject = JSONfunctions.getJSONfromURL("https://www.googleapis.com/youtube/v3/search?part=id%2Csnippet&q=natok+2015&maxResults=50&key=AIzaSyCR40QlsuX0aFfBV-wEPDsH_jxna1tDFRA");
try {
// Locate the array name in JSON
JSONArray jsonarray = jsonobject.getJSONArray("items");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
JSONObject jsonObjId = jsonobject.getJSONObject("id");
map.put("videoId", jsonObjId.getString("videoId"));
map.put ("img","http://img.youtube.com/vi/" + VIDEO_ID + "/hqdefault.jpg");
JSONObject jsonObjSnippet = jsonobject.getJSONObject("snippet");
map.put("title", jsonObjSnippet.getString("title"));
//map.put("description", jsonObjSnippet.getString("description"));
// map.put("flag", jsonobject.getString("flag"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new ListViewAdapter(MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
// Close the progressdialog
mProgressDialog.dismiss();
}
}
我需要在此处显示的所有内容,但我无法在此处显示图像.我无法找到&遇到任何错误.将JSON转换为Java对象是否有问题?
Everything i need i receive to show here but i can not show the image here.i can not find & get any kind of error.is there any problem to converting JSON to java object?
推荐答案
在这种情况下,最好使用GSON,这样无需手动即可将json字符串转换为java对象.
Its better to use the GSON in this case it will convert your json string to java object without do manual work.
将以下行添加到gradle依赖项
add following line to gradle dependency
compile 'com.google.code.gson:gson:1.7.2'
现在使用此代码将json字符串转换为java对象
Now use this code to convert the json string to java object
POJOClass obj = gson.fromJson(json, POJOClass.class)
您还可以使用此站点将json转换为POJO类,而无需手动创建
Also you can use this site to convert your json to POJO Class without creating it manually
http://www.jsonschema2pojo.org/
如果有问题,请通知我
这篇关于如何将JSON转换为JAVA对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!