Edwardd Lee

Ranch Hand
+ Follow
since Jan 17, 2017
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
2
Received in last 30 days
0
Total given
23
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Edwardd Lee

Ron McLeod wrote:It looks like a query string encoding issue.  I quickly encoded the space and quote characters and got a 200 response back.  I didn't bother trying to retrieve the returned body.



Oh that does work! Thank you! Looks like I need to learn knowledge about encoding now.
6 years ago
I just tried to use URLEncoder to encode my query. The results was very interesting - now the response code is 301 instead of 400. However, the results still turn out to be the wrong file, no matter if I set con.setInstanceFollowRedirects(); to true or false.

The output is now:
If I type the encoded URL in the browser myself, I still get the correct JSON file, not the <HTML><HEAD><TITLE>Moved Permanently</TITLE></HEAD>... file in the output. This is a very weird behavior, and I'm not sure if this is because of Google or my code...
6 years ago

Paul Clapham wrote:When I looked at Google's site for the Trends API, I found a link to the API's Terms of Service. It said that you shouldn't try to access the Trends data via anything other than their API. And you're trying to access the data via Java.

So it appears that there's some code at the back end which looks for requests like yours which come from unauthorized code. Presumably there's a way to make your requests look like they come from the authorized API but I don't know what that might be.



I am actually trying to model my code after a similar Python code which works. It seems that the Python code can access Google Trend's API without issues, but when I tried to write it in Java there are issues...
6 years ago

Carey Brown wrote:Have you tried it with just http:// instead of https:// ?



Yup and it still didn't work...
6 years ago

Carey Brown wrote:Are you going through a proxy?
Have you tried some other URL that you know should work?



No I am not.
And yes. For instance, if I just parse in https://www.google.com the response code is 200.
6 years ago
I am trying to get a JSON response from Google Trends using HTTP. This is my code snippet:

This is the output:
If I type in the URL directly in my browser, Google gives me a JSON file with no problem. However, if I try to access that URL using Java, I am given a bad request. How can I solve this problem? Thanks in advance.
6 years ago

this line will essentially be like Object k2 = new K();


Oops in the reply above I meant to type: Object k2= new Object();
What's with all the typos by me today haha.
6 years ago

Henry Wong wrote:It is a subtle point, so, I am not sure what are you trying to say?


Sorry I actually made a typo. I meant to type T() instead of T[] (though it doesn't make much difference to the question since in both cases instance(s) of T are created.)
Reading through the answers I gain some understandings are hopefully correct.

The reason why line 1 works is that although K is referenced as Object at runtime, the actual type of k1 doesn't change, so line 1 will accurately return its actual class supplied at runtime.
The reason why line 2 won't compile is that since K is referenced as Object at runtime, this line will essentially be like Object k2 = new K();, which is of little use to users, therefore Java doesn't allow this initialization altogether.
Is my understanding correct?
6 years ago
Thanks for the responses! Why, then, does test.printList(new ArrayList<String>()); returns class java.util.ArrayList, but not something like class java.util.ArrayList<String>?

Also, if actual instances at runtime does not get affected, why can we not initiate a type variable (such as new T[])?
6 years ago

The output of the code above is:

If type erasure replaces T with Object at runtime, why doesn't getClass( ) return class java.lang.Object instead for a String? Is there a difference between how type erasure works for T and SomeClass<T>?
6 years ago

Edwardd Lee wrote:
At the same time, you can still access the default, non-overriden clone () method class from Object class in any Cloneable objects since every object is the subclass of Object.



Oops I meant to say clone( ) method instead of clone( ) method class. I can't seem to edit my post so I will just correct it here...
6 years ago
Oh I finally understand. So the reason why clone( ) is set to protected is to make it useful for users who choose to override this method. protected disables objects from different packages that are no subclasses to access the user-overriden method. The initial design to give protected access modifier to clone() byJava developers was with overriding purpose in mind.

At the same time, you can still access the default, non-overriden clone () method class from Object class in any Cloneable objects since every object is the subclass of Object.

I hope my understanding is correct. Thanks again for all the responses.
6 years ago
Thanks for all your responses!

If clone() is protected, then you can call it in any object of any class, but you can only call it inside the same object.

Can you elaborate on the difference between calling a method in any object of any class and calling it inside the same object?

we don't want it to be accessible to every class in every package

But isn't every class the subclass of Object? Therefore every class can still access clone( ).
6 years ago
I don't understand why the clone( ) method in Object class is set as protected. I did some research online and the most common answer is that by setting the access modifier as protected, you can't call clone( ) in every object. But isn't a protected method still accessible to the class's subclasses, and since every class is the subclass of Object,you can call clone( ) in every object? I understand that only those classes that implement Cloneable can use clone( ) without throwing a CloneNotSupportedException, but that still doesn't explain why clone( ) is set as protected, since clone ( ) method is declared in Object class but not the Cloneable interface. Any help will be greatly appreciated.
6 years ago