Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,194,186 members, 7,953,672 topics. Date: Thursday, 19 September 2024 at 10:30 PM

Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn (1123 Views)

Help With This Json Data / Python JSON | Encoding And Decoding JSON Data With Python / How To Parse Json Data In Golang (2) (3) (4)

(1) (Reply) (Go Down)

Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by emidizy(m): 4:34pm On Jun 05, 2018
I am trying to get a json data at http://www.my currency.net/service/rates which contains a valid json data as I confirmed using my browser but returns an error 500 when I try to get the page using HttpUrlConnection. See screenshot of my code below; Note: I have been using the code to get a success response from same url but it suddenly started returning Internal Server Error.

public JSONObject MakeHttpRequest() {
try {
URL url = new URL(" http://www.mycurrency.net/service/rates " );

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestMethod( "GET" );
httpCon.connect();

Log.d("cryptox", String.valueOf(httpCon.getResponseCode() + " : " + httpCon.getResponseMessage() + " : "+ httpCon.getErrorStream()));
InputStream inputStream = new BufferedInputStream(httpCon.getInputStream());
//CREATE A BUFFERED READER TO CONVERT THE BYTES inputStream to char
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sBuilder = new StringBuilder();
String line = null;
jsonString = null;
try{
while((line = reader.readLine()) != null){
sBuilder.append(line).append("\n" );
}
jsonString = sBuilder.toString();
jsonObject = new JSONObject(jsonString);
}
catch (IOException ex){
ex.printStackTrace();
}
catch (JSONException ex){
ex.printStackTrace();
}
finally {
inputStream.close();
}
}
catch (MalformedURLException ex){
ex.printStackTrace();
}
catch(ProtocolException ex){
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}

return jsonObject;
}


LOGCAT

06-05 14:24:02.249 23278-23437/com.cyptoxchange.cryptox D/cryptox: 500 : Internal Server Error : buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@5f6823).inputStream()
06-05 14:24:02.249 23278-23437/com.cyptoxchange.cryptox W/System.err: java.io.FileNotFoundException: http://www.mycurrency.net/service/rates
06-05 14:24:02.253 17887-17887/? I/: service_to_fd shell:cat /proc/23278/stat
06-05 14:24:02.255 17887-17887/? I/: subprocess creation successful: local_socket_fd=23, pid=23717
06-05 14:24:02.271 17887-23718/? I/: post waitpid (pid=23717) status=0000
06-05 14:24:02.287 23278-23437/com.cyptoxchange.cryptox W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
06-05 14:24:02.287 23278-23437/com.cyptoxchange.cryptox W/System.err: at com.cyptoxchange.cryptox.HttpHandler.MakeHttpRequest(HttpHandler.java:32)

Any help with the cause of this will be much appreciated. Thanks.!
Re: Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by onwards: 5:43pm On Jun 05, 2018
The link returns a 404. It isn't routing to a downloadable json file.

Are you sure you are consuming a valid JSON api?
Re: Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by emidizy(m): 6:23pm On Jun 05, 2018
onwards:
The link returns a 404. It isn't routing to a downloadable json file.

Are you sure you are consuming a valid JSON api?


The link returns a valid json on my uc & chrome web browser but on opera and phx browsers it returns error 500. I m just confused. And I couldn't find find the api documentation.
Re: Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by webapi: 8:38pm On Jun 05, 2018
emidizy:
I am trying to get a json data at http://www.my currency.net/service/rates which contains a valid json data as I confirmed using my browser but returns an error 500 when I try to get the page using HttpUrlConnection. See screenshot of my code below; Note: I have been using the code to get a success response from same url but it suddenly started returning Internal Server Error.

public JSONObject MakeHttpRequest() {
try {
URL url = new URL(" http://www.mycurrency.net/service/rates " );

HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setRequestMethod( "GET" );
httpCon.connect();

Log.d("cryptox", String.valueOf(httpCon.getResponseCode() + " : " + httpCon.getResponseMessage() + " : "+ httpCon.getErrorStream()));
InputStream inputStream = new BufferedInputStream(httpCon.getInputStream());
//CREATE A BUFFERED READER TO CONVERT THE BYTES inputStream to char
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder sBuilder = new StringBuilder();
String line = null;
jsonString = null;
try{
while((line = reader.readLine()) != null){
sBuilder.append(line).append("\n" );
}
jsonString = sBuilder.toString();
jsonObject = new JSONObject(jsonString);
}
catch (IOException ex){
ex.printStackTrace();
}
catch (JSONException ex){
ex.printStackTrace();
}
finally {
inputStream.close();
}
}
catch (MalformedURLException ex){
ex.printStackTrace();
}
catch(ProtocolException ex){
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}

return jsonObject;
}


LOGCAT

06-05 14:24:02.249 23278-23437/com.cyptoxchange.cryptox D/cryptox: 500 : Internal Server Error : buffer(com.android.okhttp.internal.http.HttpConnection$ChunkedSource@5f6823).inputStream()
06-05 14:24:02.249 23278-23437/com.cyptoxchange.cryptox W/System.err: java.io.FileNotFoundException: http://www.mycurrency.net/service/rates
06-05 14:24:02.253 17887-17887/? I/: service_to_fd shell:cat /proc/23278/stat
06-05 14:24:02.255 17887-17887/? I/: subprocess creation successful: local_socket_fd=23, pid=23717
06-05 14:24:02.271 17887-23718/? I/: post waitpid (pid=23717) status=0000
06-05 14:24:02.287 23278-23437/com.cyptoxchange.cryptox W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
06-05 14:24:02.287 23278-23437/com.cyptoxchange.cryptox W/System.err: at com.cyptoxchange.cryptox.HttpHandler.MakeHttpRequest(HttpHandler.java:32)

Any help with the cause of this will be much appreciated. Thanks.!

Have you tried it on POSTMAN?
Re: Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by emidizy(m): 9:09pm On Jun 05, 2018
webapi:


Have you tried it on POSTMAN?
Not yet. I just heard about postman. How would it help ?
Re: Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by webapi: 2:23am On Jun 06, 2018
emidizy:

Not yet. I just heard about postman. How would it help ?
It helps test run json output request endpoints... Be it GET, POST, PUT, DELETE and so many other requests.
Re: Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by emidizy(m): 8:52am On Jun 06, 2018
webapi:

It helps test run json output request endpoints... Be it GET, POST, PUT, DELETE and so many other requests.

No luck with postman ! It throws same error response. See attached scrernshots. I don't understand why & how some browsers like uc web and chrome are able to pull a success response from same URL. Funny enough I tested the URL with Jsonlint (online json validator) and it validated the URL.

(1) (Reply)

Let's Team Up For A Start-up / Enugu Computer Training Center Enugu State, Nigeria Foxxymobile 07088373560 / ArtificiaI Intelligence Developer Needed ASAP

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 23
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.