Http Get/Post - Response with status: 200 for URL: null on Android


#1

I tried to make http get/post api calls with my web service(Spring+Boot), I have encountered with ‘Response with status: 200 for URL’ error.

Error log -

System.err: java.net.ConnectException: failed to connect to /***.***.*.** (port ****): connect failed: ETIMEDOUT (Con
nection timed out)
System.err:     at libcore.io.IoBridge.connect(IoBridge.java:114)
System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
System.err:     at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
System.err:     at java.net.Socket.connect(Socket.java:843)
System.err:     at com.android.okhttp.internal.Platform.connectSocket(Platform.java:152)
System.err:     at com.android.okhttp.Connection.connect(Connection.java:101)
System.err:     at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:294)
System.err:     at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
System.err:     at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
System.err:     at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:
197)
System.err:     at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.jav
a:254)
System.err:     at org.nativescript.widgets.Async$Http$RequestOptions.writeContent(Async.java:313)
System.err:     at org.nativescript.widgets.Async$Http$HttpRequestTask.doInBackground(Async.java:536)
System.err:     at org.nativescript.widgets.Async$Http$1.run(Async.java:482)
System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
System.err:     at org.nativescript.widgets.Async$PriorityThreadFactory$1.run(Async.java:52)
System.err:     at java.lang.Thread.run(Thread.java:841)
System.err: Caused by: libcore.io.ErrnoException: connect failed: ETIMEDOUT (Connection timed out)
System.err:     at libcore.io.Posix.connect(Native Method)
System.err:     at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
System.err:     at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
System.err:     at libcore.io.IoBridge.connect(IoBridge.java:112)
System.err:     ... 19 more
JS: ERROR:  Response with status: 200  for URL: null

:thinking::thinking:If i tried with sample services which i got in examples seems works fine.

this.http.get("http://dev.markitondemand.com/MODApis/Api/v2/Quote/json?symbol=" + "MS")
        .map(result => JSON.stringify(result.json()))
        .do(result => result)
        .subscribe(result => {
           console.log(result)
        }, error => {
            console.log("ERROR: ", error);
        });

What i did wrong here on making http calls!

Please suggest!


#2

I’ve created a playground code to show you how to make http get calls :slight_smile:
Hope this helps


#3

Thanks @multishiv19, this issue was happened because of API HTTPS ssl certification issue… Later I found that one.


#4

@multishiv19,
Can you explain why I am getting ‘Response with status: 200 for URL: null error’ with your sample .
on playground when I am trying to GET json file. It is not SSL site and response is not gzip:ed.

To reproduce it just make following changes home.component.ts

Line 17 -> private serverUrl = “http://rss.codeblock.fi/yllasfi.json”;

and to get more meaningfull error ->

(error) => { console.log(“error” + error.toString());

Thanks


#5

This is the error I get when I try to fetch the JSON
"message": "Http failure during parsing for (unknown url)",

This seems to be a CORS related issue.
You can find latest version of the playground here - https://play.nativescript.org/?id=8DwXF0O6V00S-Pl62ud00&template=play-ng&template=play-ng&v=2

I have used HttpClient module instead of Http module.

also you can find help related to CORS issue here


#6

@multishiv19,

I don’t believe this is CORS related, because after testing response headers:

Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Access-Control-Allow-Methods: GET,POST,OPTIONS,DELETE,PUT
Access-Control-Allow-Origin: *

There was no difference on error, but when I changed backend response header from:

"Content-Type", "application/json"

to

"Content-Type", "application/javascript"

Now with your new playground version I got response. Allthough faced another problem
"Http failure during parsing for http://rss.codeblock.fi/yllasfi.json"

Backend is now configured to response with MIME type application/javascript and you can also see that in your response dump “text” property value is valid JSON.

Why it is not accepting “Content-Type”, “application/json”?
Is it problem with JSON string, I am kinda stuck here, I can’t figure this out.

Thanks for your help.


#7

It’s still CORS issue but just setting Content-Type to application/javascript might use JSONP instead of JSON which might be solving the issue.


#8

You say it is CORS issue, but can you tell me what I should look for, since for example curl and browsers have correct response?

I have full control on backend, so I can change whatever I like.


#9

@multishiv19 @manojdcoder

This was solved.

It was CORS and file encoding issue together.

I added to two headers to backend response.

Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With
Access-Control-Allow-Origin: *

But more interesting thing was that file which was served from backend was encoded in UTF-8-BOM and when I changed it to UTF-8 with MIME type application/json, everything started to work. So this might be solution also on this error: “Unexpected token in JSON at position 0” in some cases.

Thanks for your help


#10

Awesome. Glad it worked for you :slight_smile: