Java/Yahoo Finance Retrieving Quotes, Error 401 - java

Here's my Java code to download historical Yahoo finance quotes using crumbs. Sorry, it's not the most efficient code. I'm still learning Java. Everything looks correct to me, but I get error 401 after picking up the crumb and trying to make the second access to "https"//query1.finance.yahoo.com..."
Any ideas of what I'm doing wrong?
URL myUrl = null;
try {
myUrl = new URL("https://finance.yahoo.com");
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
URLConnection urlConn = null;
try {
urlConn = myUrl.openConnection();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
urlConn.connect();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String headerName=null;
String crumb = null;
for (int i = 1; (headerName = urlConn.getHeaderFieldKey(i)) != null; i++) {
if (headerName.equals("Set-Cookie")) {
String cookie = urlConn.getHeaderField(i);
cookie = cookie.substring(0, cookie.indexOf(";"));
crumb = cookie.substring(cookie.indexOf("=") + 1, cookie.indexOf("&"));
}
}
StringBuilder qUrl = new StringBuilder();
qUrl.append("https://query1.finance.yahoo.com/v7/finance/download/AAPL");
qUrl.append("?period1=" + 1495813803L);
qUrl.append("&period2=" + 1497887408L);
qUrl.append("&interval=1d");
qUrl.append("&events=history");
qUrl.append("&crumb=" + crumb);
URL url = null;
try {
url = new URL(qUrl.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
}
URLConnection conn;
InputStream is = null;
try {
conn = url.openConnection();
String redirect = conn.getHeaderField("Location"); // Can handle one redirection
if(redirect != null ) {
conn = new URL(redirect).openConnection();
}
is = conn.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}

Related

How can I get youtubeVideo Title from URL for android studio?

I want to get the youtube video title from a url so I found this code below (IOUtils) is depreciated any other way to do this
public class SimpleYouTubeHelper {
public static String getTitleQuietly(String youtubeUrl) {
try {
if (youtubeUrl != null) {
URL embededURL = new URL("http://www.youtube.com/oembed?url=" +
youtubeUrl + "&format=json"
);
return new JSONObject(IOUtils.toString(embededURL)).getString("title");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
second way i tried
class getYoutubeJSON extends Thread {
String data = " ";
#Override
public void run() {
try {
URL url = new URL("http://www.youtube.com/oembed?url="+" https://www.youtube.com/watch?v=a4NT5iBFuZs&ab_channel=FilipVujovic"
+ "&format=json");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = bufferedReader.readLine()) != null){
data =data + line;
}
if(!data.isEmpty()){
JSONObject jsonObject = new JSONObject(data);
// JSONArray users = jsonObject.getJSONArray("author_name");
Log.d("RT " , jsonObject.toString());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
This code gets a an error Cleartext HTTP traffic to www.youtube.com not permitted
so I found this answer Android 8: Cleartext HTTP traffic not permitted but I am still getting some error I don't understand.
I solved this problem by using the volley library.
My requested url was:
String Video_id = "jhjgN2d7yok";
String url = "https://www.youtube.com/oembed?url=youtube.com/watch?v=" +Video_id+ "&format=json";

The given code is returning me an empty JSON output

void url_get(double lati,double longi) {
URL url = null;
try {
url = new URL("https://maps.googleapis.com/maps/api/place/search/json?&location="+lati+","+longi+"&radius=1000&types=hospital&sensor=true&key=MY_KEY);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection urlConnection = null;
try {
urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
TextView json = (TextView) findViewById(R.id.json3);
String theString = IOUtils.toString(in, "UTF-8");
json.setText(theString);
//System.out.println(in);
} catch (IOException e) {
e.printStackTrace();
} finally {
urlConnection.disconnect();
}
}
The code was working fine when I had hardcoded the latitude and longitude values.However when I tried to pass the values of latitutde and longitude through the function,It dint get recognized.I suppose the URL wasnt built properly.Could you please suggest a way to build my url with the values of latitude and longitude?
You should convert the values to string using:
Double.toString(double);
or:
String.valueOf(double);
So your string should be something like this:
url = new URL("https://maps.googleapis.com/maps/api/place/search/json?&location="+String.valueOf(lati)+","+String.valueOf(longi)+"&radius=1000&types=hospital&sensor=true&key=MY_KEY");
Note
In your code a " is missing at the end of the string.

Waiting a response in Thread via HttpUrlConnection

I have a Function which sends url request to server and gets data. I'm able to get data but data is coming late as request is being sent in another Thread.
I want user to wait for the request (For login authentication). How do achieve this?
private String sendRequest(final String URL) {
StrictMode.ThreadPolicy policy = new StrictMode.
ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
new Thread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
HttpURLConnection httpUrlConnection = null;
try {
httpUrlConnection = (HttpURLConnection) new URL(URL)
.openConnection();
InputStream in = new BufferedInputStream(
httpUrlConnection.getInputStream());
data = readStream(in);
Log.i(TAG,"Respose from "+URL);
System.out.println(data);
} catch (MalformedURLException exception) {
Log.e(TAG, "MalformedURLException");
} catch (IOException exception) {
Log.e(TAG, "IOException");
} finally {
if (null != httpUrlConnection)
httpUrlConnection.disconnect();
}
}
}).start();
return data;
}
I call above function like this
public JSONObject get(String URL){
data = sendRequest(URL);
try {
if(null!=data){
returnJsonObj = new JSONObject(data);
}else{
Log.i(TAG,"Response data is null");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnJsonObj;
}
How do I wait for a data in get Function.

Java / HTML : Submitting multi-line text through HTTP GET

The method I am using to get a page's content after performing a GET request is as follows:
public static String getURLContent(String URL) {
BufferedReader in = null;
String page = "";
try {
HttpClient client = getNewHttpClient();
HttpGet request = new HttpGet();
request.setURI(new URI("https://localhost/"+URL.replace(" ", "%20")));
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
page = sb.toString();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return page;
When I submit text from a multi-line textbox, an example URL would be:
staff_chat.php?action=send&staff_no=null&toStaff=123456&subject=RE:
test subject&message=----Previous message from: Stefan Dunn ----
to test
The new lines seem to prevent my code from setting the URI, it just skips and goes to the "return page" line.
How can I submit multi-line text through by GET?
Many thanks.

JSON parsing problem

i am trying to Json parsing in my android app the link is https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776
when i put it into Json object it gives errors to me
error is :
08-31 14:40:52.281: WARN/System.err(416): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject
public static String getmyproductquestiondetails(String userid,
String campaignid) {// https://www.buzzador.com/apps/present_software/webservice/index.php?op=EducationResult&userid=1&questionid=1,2,3&answergivenbyuser=1,1,0
String data = null;
try {
URL url = new URL(
"http://dignizant.com/buzz/webservice/index.php?op=getProductQuestion&userid="
+ userid + "&campaign_id=" + campaignid);
if (url.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
HttpsURLConnection https = (HttpsURLConnection) url
.openConnection();
https.setHostnameVerifier(DO_NOT_VERIFY);
http = https;
} else {
http = (HttpURLConnection) url.openConnection();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Utils utils = new Utils();
try {
data = utils.convertStreamToString(http.getInputStream());
System.out.println("getproduct details response :: " + data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
data = e.toString();
}
return data;
}
try {
JSONObject jo = new JSONObject(response);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
char[] utf8 = null;
StringBuilder properString = new StringBuilder("");
utf8 = Response.toCharArray();
for (int i = 0; i < utf8.length; i++) {
if ((int) utf8[i] < 65000) {
properString.append(utf8[i]);
}
}
System.out.println("Response of Login::"
+ properString.toString());
Had similar problem. At first my app was working great on both androids 4.0+ and 4.0- (2.3.3 2.2 etc). after a revision i have that problem. JSonarray could parse on 2.3.3
PROBLEM: Json STRING (response from server) comes with a character ' in front
so actual response= '[{"1":"omg"}] and not the correct one [{"1":"omg"}]
Solution:
if string dosent start with [ then edit response string (remove the ' character)
if (result.startsWith("["))
{
}
else
{
result= result.substring(1);
}
after then everything worked fine for me
If you are a using json-lib-2.4 as library, which I assume, you can parse strings with :
JSONSerializer.toJSON(yourString).toString()
instead of using the JsonObject class
To remove character like (\n) or unwanted character in json string used commons-lang3:3.4 library in program . i used this class to remove unwanted character in json string "StringEscapeUtils.unescapeJava(string)".
this will help you.

Categories

Resources