这是MainActivity.java文件的代码
public class MainActivity extends AppCompatActivity {
ListView lstVideo;
ArrayList<Bitmap> arrHinh= new ArrayList<Bitmap>();
ArrayList<Video> arrVideo=new ArrayList<Video>();
String playlistId="LL-5bWlJvcvfR...........";
final String API_KEY="AIza............";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstVideo=(ListView)findViewById(R.id.lstVideo);
new ParseYoutube().execute();
}
private class ParseYoutube extends AsyncTask<Void, Void, Void> {
String time, urlHinh, title, idVideo;
@Override
protected Void doInBackground(Void... params) {
if (getJsonFromUrl() != null) {
try {
JSONObject jsonObbject = new JSONObject(getJsonFromUrl());
JSONArray jsonItems = jsonObbject.getJSONArray("items");
if (jsonItems.length() > 0) {
for (int i = 0; i < jsonItems.length(); i++) {
JSONObject jsonItem = jsonItems.getJSONObject(i);
JSONObject jsonsnippet = jsonItem.getJSONObject("snippet");
title = jsonsnippet.getString("title");
time = jsonsnippet.getString("publishedAt");
JSONObject jsonThum = jsonsnippet.getJSONObject("thumbnails");
JSONObject jsondefault = jsonThum.getJSONObject("default");
urlHinh = jsondefault.getString("url");
JSONObject jsonResource = jsonsnippet.getJSONObject("resourceId");
idVideo = jsonResource.getString("videoId");
Video video = new Video();
video.setTime(time);
video.setTitle(title);
video.setUrlID(idVideo);
arrVideo.add(video);
URL url = new URL(urlHinh);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(inputStream);
arrHinh.add(bm);
}
}
} catch (JSONException | MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
lstVideo.setAdapter(new YoutubeAdapter(MainActivity.this, arrVideo, arrHinh));
}
}
public String getJsonFromUrl() {
URL url;
try {
url = new URL("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" + playlistId + "&key=" + API_KEY);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder builder = new StringBuilder();
String line = "";
while (line = bufferedReader.readLine()!== null)
{
builder.append(line + "\n");
}
inputStream.close();
return builder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是消息标签中的错误:
错误:(100,54)错误:非法开始表达
错误:任务':app:compileDebugJavaWithJavac'执行失败。
编译失败;有关详细信息,请参阅编译器错误输出。
我不知道为什么我无法调试此应用
答案 0 :(得分:0)
你在第77行给出了额外的=符号。改变它。代替它。现在你的代码将是
public class MainActivity extends AppCompatActivity {
ListView lstVideo;
ArrayList<Bitmap> arrHinh= new ArrayList<Bitmap>();
ArrayList<Video> arrVideo=new ArrayList<Video>();
String playlistId="LL-5bWlJvcvfR...........";
final String API_KEY="AIza............";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lstVideo=(ListView)findViewById(R.id.lstVideo);
new ParseYoutube().execute();
}
private class ParseYoutube extends AsyncTask<Void, Void, Void> {
String time, urlHinh, title, idVideo;
@Override
protected Void doInBackground(Void... params) {
if (getJsonFromUrl() != null) {
try {
JSONObject jsonObbject = new JSONObject(getJsonFromUrl());
JSONArray jsonItems = jsonObbject.getJSONArray("items");
if (jsonItems.length() > 0) {
for (int i = 0; i < jsonItems.length(); i++) {
JSONObject jsonItem = jsonItems.getJSONObject(i);
JSONObject jsonsnippet = jsonItem.getJSONObject("snippet");
title = jsonsnippet.getString("title");
time = jsonsnippet.getString("publishedAt");
JSONObject jsonThum = jsonsnippet.getJSONObject("thumbnails");
JSONObject jsondefault = jsonThum.getJSONObject("default");
urlHinh = jsondefault.getString("url");
JSONObject jsonResource = jsonsnippet.getJSONObject("resourceId");
idVideo = jsonResource.getString("videoId");
Video video = new Video();
video.setTime(time);
video.setTitle(title);
video.setUrlID(idVideo);
arrVideo.add(video);
URL url = new URL(urlHinh);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
Bitmap bm = BitmapFactory.decodeStream(inputStream);
arrHinh.add(bm);
}
}
} catch (JSONException | MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
lstVideo.setAdapter(new YoutubeAdapter(MainActivity.this, arrVideo, arrHinh));
}
}
public String getJsonFromUrl() {
URL url;
try {
url = new URL("https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=" + playlistId + "&key=" + API_KEY);
URLConnection urlConnection = url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder builder = new StringBuilder();
String line = "";
while (line = bufferedReader.readLine()!= null) // while (line = bufferedReader.readLine()!== null) you need to remove extra = sign
{
builder.append(line + "\n");
}
inputStream.close();
return builder.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
同意 childofsoong 。
答案 1 :(得分:0)
将while
中的getJsonFromUrl()
循环替换为 -
while ((line = bufferedReader.readLine()) != null)
{
builder.append(line + "\n");
}