I use OkHttp
to receive text from a URL
. The url is a php
script.
PHP
:
<?php
echo 'success';
?>
I get do this by the following code:
OkHttpClient hc = new OkHttpClient.Builder()
.connectTimeout(10, TimeUnit.SECONDS)
.readTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
String url = "http://hadifaridhadi.vcn.ir/com.hfapp.rokatshomar/imgofday.php";
HttpUrl.Builder httpUrlBuilder = HttpUrl.parse(url).newBuilder();
httpUrlBuilder.addQueryParameter("i", "3");
Request request = new Request.Builder()
.url(httpUrlBuilder.build())
.build();
Response response = null;
try {
response = hc.newCall(request).execute();
} catch (Throwable e) {
e.printStackTrace();
}
if (response != null) {
result = response.body().string();
}
With a browser, I receive the "success", but in my Android app, I received a garbage (PLEASE READ COMPLETELY):
It gives a link (URL) which ends with: ?i=1
, so I added this parameter with okhttp: httpUrlBuilder.addQueryParameter("i", "1");
After this, I received another garbage and that link ends with: ?i=2
, I tried that, but the server sent another garbage! A link which ends with ?i=3
. When tried this, I received ?i=4
and... (The i
goes up each time it sends a request)
What is wrong? Why can't I receive that success message?