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

5
我正在设计一个Android应用程序作为我的毕业设计项目。该应用程序需要能够连接到Web服务器。Web服务器仅是在个人计算机上本地托管的Apache Web服务器。该应用程序需要下载手机/应用程序的临时软件更新。
在设计的这一阶段,我已经建立了登录页面和主页。我的问题是如何从应用程序连接到Web服务器。我的教授还要求从登录页面,如果输入了正确的用户名和密码,这些凭据也将访问Web服务器。我也在质疑这是否可能。任何建议都将非常受欢迎。
谢谢 - 请告诉我如果这个问题需要更多信息或者不够清晰以便回答。

3
如何从应用程序连接到 web 服务器:使用 HTTP 客户端 API(如 HttpUrlConnection、OkHttp、Volley 等)。 如果输入了正确的用户名和密码,则这些凭据也将访问 Web 服务器:设置您的 Web 服务 API 使用您用于 Web 页面的相同凭证。在 HTTP 客户端 API 中使用这些凭证。 - CommonsWare
4个回答

6

将Android连接到远程服务器非常有趣,也很令人焦虑。以下链接将帮助您满足需求。

使用Php Mysql连接Android

使用servlets进行Android连接

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

谢谢!这帮了很多!! - Trapper Davis

3

我认为这对你有用

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

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

Loopj

Retrofit

Volley

okhttp

谢谢。。


1


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接