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

时间:2016-12-09 01:26:41

标签: java android

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

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

谢谢 - 如果这个问题需要更多信息或者不够明确,请告诉我。

4 个答案:

答案 0 :(得分:4)

将android连接到远程服务器非常有趣,非常焦虑。以下链接将帮助您最大限度地满足您的需求。

Android Connect with Php Mysql

Android Connection using servlets

如果您想通过使用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();

}

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

答案 1 :(得分:1)

我认为这对你有用

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

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

Loopj

Retrofit

Volley

okhttp

谢谢..

答案 2 :(得分:0)

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

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

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

https://cordova.apache.org/

答案 3 :(得分:0)