Trapper Davis
Trapper Davis

Reputation: 75

How to connect Android app to web server

I am working on designing an Android application for my senior design project. The application will need to be able to connect to a web server. The web server is just a locally hosted Apache web server on a personal computer. The application will need to download a makeshift software update to the phone/application.

At this point in the design, I have the login page and home page built. The problem I am having is how to connect to a web server from the app. My professor has also requested that from the login page, if the proper username and password have been entered, that these credentials will also access the web server. I am also questioning if this is even possible. Any advice will be greatly received.

Thanks - Please let me know if this question needs more info or is not clear enough to answer.

Upvotes: 5

Views: 56400

Answers (4)

Umesh AHIR
Umesh AHIR

Reputation: 738

I think this is use for you

As per my experience, we have lot of API calling library are available, it is free

here is a tutorial showing how to create and deploy connection.

Loopj

Retrofit

Volley

okhttp

Thanks..

Upvotes: 3

Bethan
Bethan

Reputation: 971

It's very interesting to do the connections of the android to the remote server, very anxiety. Below link will help you max for your need.

Android Connect with Php Mysql

Android Connection using servlets

And If you wanna connect the server from the android device by using HttpUrlConnection then follow the below code.

private static JSONObject get(Context ctx, String sUrl) {
HttpURLConnection connection = null;

try {

    URL url = new URL(sUrl);
    connection = (HttpURLConnection) url.openConnection();
    connection.setRequestProperty("Content-Type", "application/json");
    connection.setRequestProperty("Accept", "application/json");
    connection.setRequestProperty("Authorization",
            "Basic " + encodedAuthentication);
    connection.setRequestProperty("Accept-Charset", "utf-8,*");
    Log.d("Get-Request", url.toString());
    try {
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(connection.getInputStream()));
        StringBuilder stringBuilder = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            stringBuilder.append(line).append("\n");
        }
        bufferedReader.close();
        Log.d("Get-Response", stringBuilder.toString());
        return new JSONObject(stringBuilder.toString());
    } finally {
        connection.disconnect();
    }
} catch (Exception e) {
    Log.e("ERROR", e.getMessage(), e);
    return null;
}
}

private static String buildSanitizedRequest(String url,
                                        Map<String, String> mapOfStrings) {

Uri.Builder uriBuilder = new Uri.Builder();
uriBuilder.encodedPath(url);
if (mapOfStrings != null) {
    for (Map.Entry<String, String> entry : mapOfStrings.entrySet()) {
        Log.d("buildSanitizedRequest", "key: " + entry.getKey()
                + " value: " + entry.getValue());
        uriBuilder.appendQueryParameter(entry.getKey(),
                entry.getValue());
    }
}
String uriString;
try {
    uriString = uriBuilder.build().toString(); // May throw an
    // UnsupportedOperationException
} catch (Exception e) {
    Log.e("Exception", "Exception" + e);
}

return uriBuilder.build().toString();

}

And Json calling part should look like,

public static JSONObject exampleGetMethod(Context ctx, String sUrl, String username, String password) throws JSONException, IOException {
Map<String, String> request = new HashMap<String, String>();
request.put("username", username);
request.put("password",password);

sUrl = sUrl + "yourApiName";
return get(ctx, buildSanitizedRequest(sUrl, request));
}

Upvotes: 6

vinod
vinod

Reputation: 1126

The best way use volley library

http://www.androidhive.info/2014/05/android-working-with-volley-library-1/

Upvotes: 0

Wol Lamoseu
Wol Lamoseu

Reputation: 91

If you want REST API you can refer here

Make an HTTP request with android Sending HTTP Post Request with Android

Else if you want hybrid application you can refer here

https://cordova.apache.org/

Upvotes: 1

Related Questions