java - Grabbing JSON works from one link, not from another -


i'm doing simple json grab 2 links same code. i'm doing 2 separate times, cause of issue isn't because they're running each other or something.

here code:

@override         protected string doinbackground(object... params) {             try {                 url weatherurl = new url("my url goes here");                 httpurlconnection connection = (httpurlconnection) weatherurl                         .openconnection();                 connection.connect();                  responsecode = connection.getresponsecode();                 if (responsecode == httpurlconnection.http_ok) {                     inputstream inputstream = connection.getinputstream();                     reader reader = new inputstreamreader(inputstream);                     int contentlength = connection.getcontentlength();                     char[] chararray = new char[contentlength];                     reader.read(chararray);                     string responsedata = new string(chararray); log.v("test", responsedata); 

when try with:

http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full?alt=json

i error of having array lenth of -1

for link:

http://api.openweathermap.org/data/2.5/weather?id=5815135

it returns fine , log of of json. have idea why?

note: tried stepping through code in debug mode, couldn't catch anything. downloaded google chrome extension parsing json in browser , both urls valid. i'm out of ideas.

log this: int contentlength = connection.getcontentlength();

i don't see google url returning content-length header.

if want string output url, can use scanner , url so:

scanner s = new scanner(new url("http://www.google.com").openstream(), "utf-8").usedelimiter("\\a"); out = s.next(); s.close(); 

(don't forget try/finally block , exception handling)

the longer way (which allows progress reporting , such):

string convertstreamtostring(inputstream is) throws unsupportedencodingexception {        bufferedreader reader = new bufferedreader(new                                   inputstreamreader(is, "utf-8"));       stringbuilder sb = new stringbuilder();       string line = null;       try {           while ((line = reader.readline()) != null)               sb.append(line + "\n");       } catch (ioexception e) {           // handle exception       } {           try {               is.close();           } catch (ioexception e) {               // handle exception           }       }       return sb.tostring();    } } 

and call string response = convertstreamtostring( inputstream );


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -