我正在尝试从重定向的页面获取JSON,但是现在代码拒绝遵循重定向,即使我特别要求它遵循代码中的重定向。由于这是Android,我在后台线程上运行此函数。它正在努力重定向,此时它会卡在“解析网址”上(有关此特定网址的详细信息,请查看此处:https://developers.soundcloud.com/docs/api/reference#resolve)。这是方法:
class SoundcloudTrackIDAsyncTasker extends AsyncTask<URL, Void, Integer> {
@Override
protected Integer doInBackground(URL... params) {
try {
URLConnection newURLConnection = params[0].openConnection();
HttpURLConnection conn = (HttpURLConnection) newURLConnection;
conn.setInstanceFollowRedirects(true); // fails, regardless if true or false
HttpURLConnection.setFollowRedirects(true); // same
newURLConnection.connect();
InputStream newURLConnectedStream = newURLConnection.getInputStream();
System.out.println("" + newURLConnection);
URL tracksURL =
new URL(newURLConnection + "?client_id=" + CLIENT_ID);
URLConnection trackURLConnection = tracksURL.openConnection();
trackURLConnection.connect();
InputStream trackURLConnectionInputStream = trackURLConnection.getInputStream();
InputStreamReader readHTTPStream = new InputStreamReader(trackURLConnectionInputStream,
Charset.forName("UTF-8"));
Gson gson = new Gson();
//read the track ID and return it.
JSONURL urlAsJson = gson.fromJson(readHTTPStream, JSONURL.class);
return urlAsJson.getTrack();
} catch (IOException e) {
return null;
}
}
}