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

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

本文介绍了如何将Android应用连接到Web服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

在设计的这一点上,我已经建立了登录页面和主页.我遇到的问题是如何从应用程序连接到Web服务器.我的教授还要求从登录页面输入正确的用户名和密码,这些凭据也将访问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.

谢谢-如果这个问题需要更多信息,或者不清楚是否可以回答,请告诉我.

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

推荐答案

进行android与远程服务器的连接非常有趣,非常焦虑.下面的链接将帮助您最大程度地满足您的需求.

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与Php Mysql连接

使用servlet的Android连接

如果您想使用HttpUrlConnection从android设备连接服务器,请遵循以下代码.

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

}

Json调用部分应该看起来像

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

这篇关于如何将Android应用连接到Web服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 19:51