I try to read data from azure website for my android application. But when i try to reach it gives me an error.
But when i look with this website i can see json data.
I can see all json data so truely. But somehow when i enter the url my browser i can't see the data. And my url is http://tcm-test-api.azurewebsites.net/api/movie/search?term=%22Batman%22&page=1
Try setting Content-Type to application/json;charset=utf-8 in your Http request header. In android when you create the HttpUrlConnection do it as follows
URL myURL = new URL(serviceURL);
HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
myURLConnection.setRequestMethod("GET");
myURLConnection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
Related
I has an android app that AndroidManifest set to usesCleartextTraffic="true" because it is using http to connection to server.
But, now i plan to update the server to use HTTPS, and the server will redirect all http to https.
So, i want to know, if server redirect all traffic to HTTPS, will the android app that access the server using HTTP still working?
Example i am using the java code below to access server content. So will app still able to read the content at the app.php, if server update to use HTTPS? (server set to redirect all http to https)
String urls = "http://www.example.com/app.php";
URL url = new URL(urls);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Cache-Control", "no-cache");
content = readStream(conn.getInputStream());
Thanks in advance
I am trying to connect a http webpage using the following codes in Android system. The http webpage can not be connected, but https such as https://wwww.google.com can be successfully connected. Using getHeaderFields(), can get correct header for https://wwww.google.com, but get {} for http websites.
String link = "http://www.android.com/";
URL url = new URL(link);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
I have a server that only opens a port using port number 443. I need to upload a file to that server using Android client. My question is:
is it possible to connect and upload to such server using httpsurlconnection or do i need to use other class?
if it is possible, can you please show me an example of it?
thanks.
You should be able to do that simply by creating a URL with 'https' instead of 'http'.
Example from documentation:
URL url = new URL("https://www.android.com/");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
} finally {
urlConnection.disconnect();
}
Refer to the documentation for details:
https://developer.android.com/training/articles/security-ssl.html#HttpsExample
https://developer.android.com/reference/java/net/HttpURLConnection.html
I am still a beginner in Android. I am currently sending large amount of data(json) from the android app to web server using HTTPClient. I found that HttpURLConnection supports decompression for request but is there any support or any way that I can upload a compress/gzipped json to the web server?
Is the only way to achieve this is to manually gzip the json string and put the gzipped string into in to the post?
You can add GZIP compression in your HTTP header.
HttpUriRequest request = new HttpGet(url);
request.addHeader("Content-Encoding", "gzip");
// ...
httpClient.execute(request);
You can check this link for more information
I know how to check if my apps is connecting to wifi, and get XML string and parse it to my apps, but i want to know how to check if a site with xml ready to parse is already activate from the server so that i can get the XML and parse, because if that site is not activated from the server ill get the error "No Route to Host".
How to Check if that link is ready and activated from the server?
When resource was not found, your web server should set response code to 404.
If you're using HttpUrlConnection
URL url = new URL("www.example.com");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if(conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND ){
handleError(); // your custom error handler
}