以下是网址JSON
我想解析JSON并从链接中获取title
并将其存储到String中。
我尝试了其他JSON解析示例,但每次尝试这些示例时,它都无法正常工作或我的应用崩溃。
代码 -
//the components are in separate methods
web = (WebView)this.findViewById(R.id.webView1);
protocol = "https://www.youtube.com/";
web.getSettings().setJavaScriptEnabled(true);
WebSettings settings = web.getSettings();
settings.setSupportMultipleWindows(false);
web.setWebChromeClient(new WebChromeClient());
web.setWebViewClient(new WebViewClient());
web.loadUrl(protocol);
web.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Request request = new Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
}
});
//Adding download manager to it. Download manager downloads the youtube video but the title is changed.
//I have action bar button, on click of which the download url of current weburl, converts it into mp3 and redirects user to the download page.
case R.id.action_download:
String aJsonString = "ffff";
final String webUrl = web.getUrl();
String z = webUrl.substring(30);
protocol = "http://www.youtube-mp3.org/?e=session_expired&t#v=" + z;
web.loadUrl(protocol);
String json = "http://gdata.youtube.com/feeds/api/videos/" + z + "?v=2&alt=jsonc";
getData();
//calling your method
return true;
//your method
public void getData()
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/videos/XbGs_qK2PQA?v=2&alt=jsonc");
try
{
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response = EntityUtils.toString(resEntity);
JSONObject json = new JSONObject(_response);
JSONObject data = json.getJSONObject("data");
String tile = data.getString("title");
Toast.makeText(this, tile, Toast.LENGTH_LONG).show();
}catch(Exception e)
{
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
}
答案 0 :(得分:3)
你的json
{ // json object node
"apiVersion": "2.1",
"data": { // json object data
"id": "XbGs_qK2PQA",
"uploaded": "2013-11-27T16:50:00.000Z",
"updated": "2014-08-15T12:08:36.000Z",
"uploader": "eminemvevo",
"category": "Music",
"title": "Eminem - Rap God (Explicit)", // your title key value pair
获得头衔
JSONOBject json = new JSONOBject("json string");
JSONObject data = json.getJSONObject("data");
String title = data.getString("title");
你也可以考虑使用google gson
如何将该网址中的JSON转换为字符串?
class TheTask extends AsyncTask<Void,Void,Void>
{
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
getData();
return null;
}
}
你的getData
public void getData()
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("http://gdata.youtube.com/feeds/api/videos/XbGs_qK2PQA?v=2&alt=jsonc");
try
{
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
String _response=EntityUtils.toString(resEntity);
JSONOBject json = new JSONOBject(_response);
JSONObject data = json.getJSONObject("data");
String tile = data.getString("title");
}catch(Exception e)
{
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
}
调用
new TheTask().execute();