I use java to get youtube video url from a youtube url,it seems like i should decode video url twice to get the original video url,but something is wrong when i access the video url which i get from my code. Im sure i can access any video url which i get when i first run the code after i finish the code, but after that ,i can not access any video url i get no matter the video url is from decode first or twice. I suppose maybe only youtube itself can access the video url i get or the server blocked my IP. So i need you guys to help me to tell me whats wrong and how i could do to get the video url that can play, thx. My code and answer is below:
try {
String ytUrl = "https://www.youtube.com/watch?v=Cj3AV92fJ90";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(ytUrl);
HttpResponse response = client.execute(request);
String html = "";
InputStream in = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
str.append(line.replace("\\u0026", "&"));
}
in.close();
html = str.toString();
String val=RegexUtil.find(html,"stream_map\":.*?\"(.*?)\"",1);
if (!StringUtils.isEmpty(val)) {
String url = URLDecoder.decode(val, "UTF-8");
System.out.println("1 decode url: "+url);
url = URLDecoder.decode(url, "UTF-8");;
System.out.println("2 decode url: "+url );
}
} catch (Exception e) {
e.printStackTrace();
}
1 decode url: url=https://r4---sn-i3b7knlk.googlevideo.com/videoplayback?id=o-AE4MBDOnLdC4X0a7wjJ63diBNkBpJ2XiuejUOXrB8onv&dur=421.442&mime=video%2Fmp4&fvip=4&requiressl=yes&ms=au%2Conr&mt=1540798558&ratebypass=yes&itag=22&pl=25&sparams=dur%2Cei%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Crequiressl%2Csource%2Cexpire&source=youtube&mv=m&mn=sn-i3b7knlk%2Csn-npoeene7&lmt=1537570091802082&key=yt6&ei=8LjWW9P1Ns2R8gOGhKO4AQ&c=WEB&expire=1540820305&ip=114.113.240.105&ipbits=0&initcwndbps=362500&mm=31%2C26&itag=22&type=video/mp4; codecs="avc1.64001F, mp4a.40.2"&s=C8C80C74C92B4306498EE1183D683A0E60962F69BD.4F1AE9CE009B83425F389362CFC2FE23A45948D5&quality=hd720&sp=signature
2 decode url: url=https://r4---sn-i3b7knlk.googlevideo.com/videoplayback?id=o-AE4MBDOnLdC4X0a7wjJ63diBNkBpJ2XiuejUOXrB8onv&dur=421.442&mime=video/mp4&fvip=4&requiressl=yes&ms=au,onr&mt=1540798558&ratebypass=yes&itag=22&pl=25&sparams=dur,ei,id,initcwndbps,ip,ipbits,itag,lmt,mime,mm,mn,ms,mv,pl,ratebypass,requiressl,source,expire&source=youtube&mv=m&mn=sn-i3b7knlk,sn-npoeene7&lmt=1537570091802082&key=yt6&ei=8LjWW9P1Ns2R8gOGhKO4AQ&c=WEB&expire=1540820305&ip=114.113.240.105&ipbits=0&initcwndbps=362500&mm=31,26&itag=22&type=video/mp4; codecs="avc1.64001F, mp4a.40.2"&s=C8C80C74C92B4306498EE1183D683A0E60962F69BD.4F1AE9CE009B83425F389362CFC2FE23A45948D5&quality=hd720&sp=signature
Related
I write a program to capture the JSON response from the server which contain some needed information I needed. I discovered that sometime my program will not able to capture the correct JSON string and sometime it's works well with no problem. I try to check my code for capturing the response and have no idea on it. When I check the JSON string from server, it's contain the field I want but my program not able to capture the correct data.
This is my JSON String
"info":{
"reason":"Fine",
"boolean":false,
"post":{
"actions":"",
"actions_In_process":"Checked",
"destination":"C%3ApdfFdoc%20edipdfFdestinationpdfFexample.pdf",
"file_type":"pdf",
},
This is my program for capture the JSON string and the field I need is action_In_process
String Url1 = "http://IP:port/etc/";
HttpURLConnection con = (HttpURLConnection) Url1.openConnection();
con.setRequestMethod("GET");
con.connect();
int responseCode = con.getResponseCode();
if(responseCode == 200)
{
try
{
InputStream is = con.getInputStream();
BufferedReader read = new BufferedReader (new InputStreamReader(is));
StringBuffer buffer = new StringBuffer();
String data = "" ;
while((data = read.readLine() ) != null )
{
buffer.append(data);
}
String JsonData = buffer.toString();
JSONObject jobj = new JSONObject(JsonData);
JSONObject process_info = jobj.getJSONObject("info");
JSONObject pi = process_info.getJSONObject("post");
String action_run = pi.getString("actions_In_process");
System.out.println("Process:" +action_run);
What I had found out is sometime the Process showing is blank but when I get back the JSON data and I found out the field I need is inside the JSON response. Please share your opinion on this issues
This is the message showing my compiler if I not able to capture the correct JSON string
Process :
If in normal condition
Process : check
BufferedReader's readline() is blocking.
I am making request to an api which sends back response as json data. But sometimes it sends back an html page which has api documentation. In documentation it's nowhere mentioned that the api can send a different response that json. There is no pattern as to when it sends json and when Html page. Sometimes same request sends back HTML and other times json response. I want to know what could be possible reasons of this exception. Is it problem with APi or my code.
I am using below code to fetch the response
URI uri = new URI(url);
BufferedReader b = new BufferedReader(new InputStreamReader(uri.toURL().openStream()));
while ((line = b.readLine()) != null)
{
s.append(line);
}
tokener = new JSONTokener(s.toString());
EDIT
It is probably because of the Accept header in the HTTP request not being set. Try the following:
URI uri = new URI(url);
URLConnection httpCon = uri.toURL().openConnection();
httpCon.setRequestProperty("Accept", "application/json");
BufferedReader b = new BufferedReader(new InputStreamReader(httpCon.getInputStream()));
while ((line = b.readLine()) != null)
{
s.append(line);
}
tokener = new JSONTokener(s.toString());
I am trying to hit the URL and get the response from my Java code.
I am using URLConnection to get this response. And writing this response in html file.
When opening this html in browser after executing the java class, I am getting only google home page and not with the results.
Whats wrong with my code, my code here,
FileWriter fWriter = null;
BufferedWriter writer = null;
URL url = new URL("https://www.google.co.in/?gfe_rd=cr&ei=aS-BVpPGDOiK8Qea4aKIAw&gws_rd=ssl#q=google+post+request+from+java");
byte[] encodedBytes = Base64.encodeBase64("root:pass".getBytes());
String encoding = new String(encodedBytes);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
connection.setRequestProperty("Accept-Charset", "UTF-8");
connection.setDoInput(true);
connection.setRequestProperty("Authorization", "Basic " + encoding);
connection.connect();
InputStream content = (InputStream) connection.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
try {
fWriter = new FileWriter(new File("f:\\fileName.html"));
writer = new BufferedWriter(fWriter);
while ((line = in.readLine()) != null) {
String s = line.toString();
writer.write(s);
}
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Same code works couple of days back, but not now.
The reason is that this url does not return search results it self. You have to understand google's working process to understand it. Open this url in your browser and view its source. You will only see lots of javascript there.
Actually, in a short summary, google uses Ajax requests to process search queries.
To perform required task you either have to use a headless browser (the hard way) which can execute javascript/ajax OR better use google search api as directed by anand.
This method of searching is not advised is supposed to fail, you must use google search APIs for this kind of work.
Note: Google uses some redirection and uses token, so even if you will find a clever way to handle it, it is ought to fail in long run.
Edit:
This is a sample of how using Google search APIs you can get your work done in reliable way; please do refer to the source for more information.
public static void main(String[] args) throws Exception {
String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=";
String search = "stackoverflow";
String charset = "UTF-8";
URL url = new URL(google + URLEncoder.encode(search, charset));
Reader reader = new InputStreamReader(url.openStream(), charset);
GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
// Show title and URL of 1st result.
System.out.println(results.getResponseData().getResults().get(0).getTitle());
System.out.println(results.getResponseData().getResults().get(0).getUrl());
}
I am making desktop app in java for instagram right now and i don't know how to get code from callback url. My app is desktop so my REDIRECT-URI is http://instagram.com. So in my app i send a request to https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code and get redirected to instagram.com?code=CODE. And also forgot to mention, I get a code once by my hands a i gave all permissions. So now then i make request i redirected straightly to URL with code.
How do i get code from callback uri in program in java? This is my code:
static void GetCode()
{
try {
String url = "https://api.instagram.com/oauth/authorize/?client_id=" + ClientId + "&redirect_uri=" + BackUri + "&response_type=code&scope=likes+comments+relationships";
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result.toString());
}
catch(Exception e)
{
System.out.print("GET CODE ERROR->"+e.toString());
}
}
Change REDIRECT_URI to http://localhost. So the actual 'redirection' code should be done on your side. And you can parse value of argument code. Also look at the answers to this question. This will help you to grab URI with the code.
I found this great tutorial on how to use JSON to retrieve Twitter updates, and post it in a TextView:
http://www.ibm.com/developerworks/xml/library/x-andbene1/
I've followed this tutorial step by step, so my code is the same.
In the method examineJSONFile(), we have this line:
InputStream is = this.getResources().openRawResource(R.raw.jsontwitter);
This file is downloaded directly from the Twitter website, as mentioned in the second paragraph of http://www.ibm.com/developerworks/xml/library/x-andbene1/#aotf.
All this is great, except for one thing: it's absolutely no use that one has to download the Twitter updates (tweets) and then build the app using this as a raw file. It should be possible to download this JSON file at runtime, and then show the tweets in the TextView afterwards.
I have tried to create the InputStream in another way, like this:
String url = "http://twitter.com/statuses/user_timeline/bbcnews.json";
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet(url));
InputStream is = response.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(is,"UTF-8"));
StringBuffer sb = new StringBuffer();
try
{
String line = null;
while ((line = br.readLine())!=null)
{
sb.append(line);
sb.append('\n');
}
}
catch (IOException e)
{
e.printStackTrace();
}
String jsontext = new String(sb.toString());
But it seems this line: HttpResponse response = httpclient.execute(new HttpGet(url)); throws an exception.
Any help please?
You seem to be missing the INTERNET permission. Look at the logs and it would be clear what exactly is the problem.