Java 如何将 Android 应用程序连接到 Web 服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41051686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:07:01  来源:igfitidea点击:

How to connect Android app to web server

javaandroid

提问by Trapper Davis

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.

我正在为我的高级设计项目设计一个 Android 应用程序。该应用程序需要能够连接到 Web 服务器。Web 服务器只是个人计算机上本地托管的 Apache Web 服务器。该应用程序需要将临时软件更新下载到手机/应用程序。

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.

在设计的这一点上,我已经构建了登录页面和主页。我遇到的问题是如何从应用程序连接到 Web 服务器。我的教授还要求从登录页面,如果输入了正确的用户名和密码,这些凭据也将访问网络服务器。我也在质疑这是否可能。任何建议都会很受欢迎。

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

谢谢 - 如果这个问题需要更多信息或不够清楚无法回答,请告诉我。

回答by Wol Lamoseu

If you want REST API you can refer here

如果你想要 REST API,你可以参考这里

Make an HTTP request with androidSending HTTP Post Request with Android

使用 android发出HTTP 请求 使用 Android发送 HTTP Post 请求

Else if you want hybrid application you can refer here

否则,如果你想要混合应用程序,你可以参考这里

https://cordova.apache.org/

https://cordova.apache.org/

回答by Bethan

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到远程服务器的连接很有意思,很着急。下面的链接将帮助您最大限度地满足您的需求。

Android Connect with Php Mysql

Android 连接 PHP Mysql

Android Connection using servlets

使用 servlet 的 Android 连接

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

如果您想使用 HttpUrlConnection 从 android 设备连接服务器,请按照以下代码操作。

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,

而 Json 调用部分应该看起来像,

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));
}

回答by Umesh AHIR

I think this is use for you

我认为这对你有用

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

根据我的经验,我们有很多 API 调用库可用,它是免费的

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

这是一个教程,展示了如何创建和部署连接。

Loopj

循环

Retrofit

改造

Volley

凌空抽射

okhttp

好http

Thanks..

谢谢..