₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,331,128 members, 8,448,818 topics. Date: Monday, 20 July 2026 at 09:09 PM

Toggle theme

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

Nairaland ForumScience/TechnologyProgrammingHelp ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn (1307 Views)

1 Reply (Go Down)

Help ! Json Data Displays On Browser But Returns Error (500) Via Httpurlconn by emidizy(op):
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(op): 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(op): 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(op):
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

Help With This Json DataPython JSON | Encoding And Decoding JSON Data With PythonHow To Parse Json Data In Golang234

I Need A Very Good Hacker For A Very Interesting Job Life ChangingDon't Be A Framework-infected Programmer10 Or More Programmers Needed To Co-found A Big Technology Company