I tried to read the csv file inside Google Drive through the link.
But it's accessible or impossible. If it return the requestCode, it will also return '200', but in most cases it will return '403'.
I thought about it for a few days, but I don't know why I can't approach it.
Access to the link through the browser is possible without any problems, but access through the Android studio has occurred with errors.
This is sharing settings for my Google Drive files.
public String getPlaylistData(String selectmood){
try {
String pid = "1-5******Ok1iHPVzy2q-Ns"; // url
HttpsURLConnection urlConnection = null;
URL stockURL = new URL("https://drive.google.com/uc?export=view&id=" + pid);
urlConnection = (HttpsURLConnection) stockURL.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
System.out.println("ResponseCode: " + urlConnection.getResponseCode());
// error here!
BufferedReader in = new BufferedReader(new InputStreamReader(stockURL.openConnection().getInputStream()));
CSVReader reader = new CSVReader(in);
String[] nextline;
Integer j = 0;
while ((nextline = reader.readNext()) != null) {
if (!nextline[Category.Playlist_Mood.number].equals(selectmood)) {
continue;
}
moodselect.add(nextline[Category.Playlist_ID.number]);
}
in.close();
if (urlConnection.getErrorStream() != null){
try {
urlConnection.getErrorStream().close();
}catch (IOException e){
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return moodselectid_result;
}
And this is my logcat.
I/System.out: ResponseCode: 403
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
W/System.err: java.io.FileNotFoundException: https://doc-00-2k-docs.googleusercontent.com/docs/securesc/ha0ro937gcuc7l7deffksulhg5h7mbp1/titjmodt74qf2tnb2i423gujafcd4k2a/1649251650000/05483575271960789860/*/1-5**********E3Ok1iHPVzy2q-Ns?e=view
W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251)
W/System.err: at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
W/System.err: at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
W/System.err: at com.example.cultureforyou.CSVStreamingActivity.getPlaylistData(CSVStreamingActivity.java:202)
W/System.err: at com.example.cultureforyou.CSVStreamingActivity.lambda$ThreadNetwork$0$com-example-cultureforyou-CSVStreamingActivity(CSVStreamingActivity.java:162)
W/System.err: at com.example.cultureforyou.CSVStreamingActivity$$ExternalSyntheticLambda0.run(Unknown Source:4)
W/System.err: at java.lang.Thread.run(Thread.java:764)
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
I tried the following methods. but not works.
1. Added the following settings:
urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");
- Added the following code (AndroidManifest.xml):
android:usesCleartextTraffic="true"
- I changed the code where the error occurs to the code below.
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getURL().openStream()));