how to know file exist on server or not in android - android

I want to know if a specific file exists a on server or not. For example, suppose I have an .xml file on my server and I want to know if the file is there or not through java from my android application.

What server is it? If its HTTP server, you can request that file and check from response if it exist. If its custom server you have to implement that feature yourself.

You could use something like this:
URL url = null;
URLConnection connection = null;
InputStreamReader stream = null;
// Parse the URL
try {
url = new URL(urlString);
} catch (MalformedURLException e1) {
System.out.println("Malformed URL");
return null;
}
// Open a connection to the URL
try {
connection = url.openConnection();
} catch (IOException e) {
System.out.println("Could not create connection");
return null;
}
// Get the remote file
try {
stream = new InputStreamReader(connection.getInputStream());
} catch (IOException e) {
System.out.printf("File \"%s\" does not exist!",urlString);
return null;
}

Related

how to update or download the json file from url request to a local directory?

I am trying to make a mobile application, the data i am using is a huge nested JSON object which is a live API on some website. Instead of calling URL request to get the data, i have downloaded the json file and stored it on my asset folder in android. As the data was so huge thats why I stored it locally in the asset folder to avoid a lag in a request. Problem is, i have stored it locally and if the data get updated in website it wont come up to me. i want to make some kind of a method where an app user can update the data itself.
public void updatingJSONFile(){
HttpURLConnection urlConnection;
InputStream in = null;
try{
//the url we wish to connect to
URL url = new URL("my URL");
//open the connnection to the specific url
urlConnection = (HttpURLConnection) url.openConnection();
//get the response from the server in the input stream
in = new BufferedInputStream(urlConnection.getInputStream());
}catch(IOException e){`enter code here`
e.printStackTrace();
}
// convert the input stream to a string
String response = convertStreamToString(in);
// print the response to the android monitor/logcat
System.out.print("Server response = " + response);
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(response);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}

How to get XML data from https url in Android

I want to get XML data from the web server https://ruralfire.qld.gov.au/bushfirealert/bushfireAlert.xml
However, I can't do it because my codes always have an error "javax.net.ssl.SSLHandshakeException: Connection closed by peer".
And my InputStream is always null, so I can't do anything with it (such as parsing).
I ensure that the problem is in my connection but I don't know how to solve it.
This is my code in connecting to the web server:
private InputStream downloadUrl(String urlString) throws IOException {
URL url;
url = new URL(urlString);
InputStream is = null;
try {
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setDoInput(true);
is = con.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return is;
}
May you help me to solve this problem ?
Thank you so much.

How to Connect Android Device with web serve using AsyncTask

I have this asynctask.I need to connect device with web server.Need to send a JSON Arry and receive JSON array. Can i use httpUrlConnection ? or httpClient. Does httpClient support latest versions of Android?
class background_thread extends AsyncTask<JSONArray, Void, Boolean> {
protected Boolean doInBackground(JSONArray... params) {
//connect with server side php script
String UR = "127.0.0.1/abc/index.php";
try {
URL url = new URL(UR);
try {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
json_array=json_encode();
conn.setDoOutput(true);
conn.setChunkedStreamingMode(0);
OutputStream out = new BufferedOutputStream(conn.getOutputStream());
writeStream(out);
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
return true;
}
You should use google's own volley library to make network calls and handle the response data in model classes. This is the most modular approach. Refer this link for official documentation.

Connecting an android phone to a server over http and outputstream

I am trying to connect an android phone to a server running on my local machine over http and outputstream the content but unsuccessful..!
here is my code :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
URL url = new URL("http://192.168.24.42:80/DebugServer/");
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
private void readStream(InputStream in) {
try {
in.read();
in.read();
} catch (IOException e) {
e.printStackTrace();
}
}
URL url = new URL("http://192.168.24.42:80/DebugServer/");
is your connection URL. Change the port to 8080 because that is where the server listens to.
Also, did you go to the bin folder of your tomcat (I am assuming that here) and run the startup.BAT or startup.SH file because without that your server is not running.
To check if your server is properly running, try http://localhost:8080
And please, post the logcat :)

Android: Quick web requests

For my application I need to have the latest data from an webpage that is hosted on a server on my local network.
So I request the latest page with a HTTP GET and when the data is received, I send another request.
With my current implementation I reach around the 100 - 120 ms per request. Is there a possibility to make this quicker because it's the same url that is requested.
For example keep the connection open to the page and grep the latest data without setting up a new connection?
This page is around the 900-1100 bytes.
HTTP get code:
public static String makeHttpGetRequest(String stringUrl) {
try {
URL url = new URL(stringUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setReadTimeout(300);
con.setConnectTimeout(300);
con.setDoOutput(false);
con.setDoInput(true);
con.setChunkedStreamingMode(0);
con.setRequestMethod("GET");
return readStream(con.getInputStream());
} catch (IOException e) {
Log.e(TAG, "IOException when setting up connection: " + e.getMessage());
}
return null;
}
Reading inputstream
private static String readStream(InputStream in) {
BufferedReader reader = null;
StringBuilder total = new StringBuilder();
try {
String line = "";
reader = new BufferedReader(new InputStreamReader(in));
while ((line = reader.readLine()) != null) {
total.append(line);
}
} catch (IOException e) {
Log.e(TAG, "IOException when reading InputStream: " + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return total.toString();
}
As I know there isn't an implementation like you are asking for. I've been dealing a lot with http requests and the best thing you can do is your code. There is another thing which need some attention...your connection maybe slow and depending on that connection time can be more or in some cases which I've been dealing a lot the connection's timeout isn't enough big, but that's server problem.
In my opinion you should use what you have now.

Categories

Resources