简体   繁体   English

连接到远程服务器的Android应用程序

[英]Android Application that connects to remote server

I am trying to build an Android App on Platform 2.2 Froyo. 我正在尝试在2.2平台Froyo上构建一个Android应用程序。 The app is supposed to connect to a remote server, fetch data from it and display to user in a different language. 该应用程序应该连接到远程服务器,从中获取数据并以其他语言显示给用户。

  • So my question - What technologies I need to learn so that I can build the above app. 所以我的问题是-我需要学习什么技术才能构建上述应用程序。

Note - I have already installed the Android platform and have built simple apps like Hello, world. 注意-我已经安装了Android平台,并已构建了诸如Hello,world之类的简单应用。 I know Java. 我知道Java。 Also I am using Eclipse. 我也在使用Eclipse。

Thank you for your answers. 谢谢您的回答。 No rude comment please... 请不要粗鲁的评论...

//--------------Code for connecting to web using HTTP protocol-----------------// // --------------使用HTTP协议连接到Web的代码----------------- //

package in.androidbook.Networking;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity 
{
    ImageView img;
    /* This is for making asynchronous calls to ensure that connection to server will not return until data is received */

    private class BackgroundTask extends AsyncTask<String, Void, Bitmap>
    {
        protected Bitmap doInBackground(String...url)
        {
            Bitmap bitmap = DownloadImage(url[0]);
            return bitmap;
        }

        protected void onPostExecute(Bitmap bitmap)
        {
            ImageView img = (ImageView) findViewById(R.id.img);
            img.setImageBitmap(bitmap);         
        }
    }
    // Code for making HTTP connection
    private InputStream OpenHttpConnection(String urlString) throws IOException 
    {
        InputStream in = null;
        int response = -1;

        URL url = new URL(urlString);//We take an object of class URL
        URLConnection conn = url.openConnection(); //Create a connection object and open the connection

        if(!(conn instanceof HttpURLConnection)) throw new IOException("Not an Http connection");
        try
        {
            HttpURLConnection httpConn = (HttpURLConnection) conn; //httpConn object is assigned the value of conn. Typecasting is done to avoid conflict.
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            httpConn.connect();
            response = httpConn.getResponseCode();

            if(response == HttpURLConnection.HTTP_OK)
                in = httpConn.getInputStream();
        }
        catch (Exception ex)
        {
            throw new IOException("Error connecting");
        }
        return in;
    }   
        //------------------------------------------ OpenHttpConnection method completed----------------------------------------------------------//
    //----------------------------------------------------------------------------------------------------------------------------------------------------------------//
    //-------------------------------Method to download an image--------------------------------------------------------------------------------------//
    private Bitmap DownloadImage(String URL)
    {
        Bitmap bitmap = null;
        InputStream in = null;
        try
        {
            in = OpenHttpConnection(URL);
            bitmap = BitmapFactory.decodeStream(in);
            in.close();
        }
        catch(IOException e1)
        {
            Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show();
            //Toast displays a short msg to user. this refers to current object.
            e1.printStackTrace();
        }
        return bitmap;
    }  


/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

       Bitmap bitmap = DownloadImage("http://i.zdnet.com/blogs/3-29-androids.jpg");
        img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
    }
}

UPDATED (based on comment) : As we are talking about the client side, I confirm my answer. 更新(基于评论):当我们谈论客户端时,我确认我的回答。 If the site is not yours, the first thing you need to do if check how/if it allows for some kind of communication via API, and in what kind of format (XML and JSON being the most used). 如果该网站不是您的网站,则首先需要检查它如何/是否允许通过API进行某种形式的通信以及采用哪种格式(最常用XML和JSON)。 With this information, it should be pretty easy. 有了这些信息,它应该很容易。 Take a look of the Android example using the Google Map or Twitter, you should find some. 看一下使用Google Map或Twitter的Android示例,您应该找到一些示例。

Well, it depends what do you mean exactly : are you asking for the skills needed on the client side (the app) - in the idea that the server is already built, or will be by someone else, or the skills needed for the server ? 好吧,这取决于您的确切意思是:您是否在要求客户端(应用程序)所需的技能-是因为服务器已经构建或将由其他人构建,还是服务器所需的技能? ?

In the former case, I would advice to communicate with REST API and JSON. 在前一种情况下,我建议与REST API和JSON进行通信。 Take a look at apache HTTPGet, HTTPClient and HTTPResponse class and org.json (all are included in Android). 看一下Apache HTTPGet,HTTPClient和HTTPResponse类以及org.json(所有都包含在Android中)。 If you want to test with them, use some public APIs (so you do not have to worry about the server), such as Google Map API (which is simple and free to use under some limits). 如果要使用它们进行测试,请使用一些公共API(这样就不必担心服务器),例如Google Map API(它很简单,在某些限制下可以免费使用)。

In the latter case, I'm with ColWinters : if you know java, use it there also, with Tomcat as a server and a basic Servlet. 在后一种情况下,我使用ColWinters:如果您知道Java,也可以在其中使用它,并将Tomcat作为服务器和基本的Servlet。 You'll find examples aplenty on the Internet. 您会在Internet上找到大量示例。

Look up these technologies, 查找这些技术,

Apache Tomcat - Java Server Pages (server processing) MySQL (storage of data) Apache Tomcat-Java服务器页面(服务器处理)MySQL(数据存储)

and thats it. 就是这样。 Also make sure to do the request in a seperate thread like an Async Task from an activity. 另外,请确保在单独的线程(例如来自活动的异步任务)中执行请求。

If you know java, I would suggest servlet as service hosted on server which reads data from mysql or anydatabase and contructs as json. 如果您知道Java,我建议将servlet作为服务托管在服务器上,该服务器从mysql或anydatabase读取数据并将其构造为json。 in your android app make http using in buit httpclient to remote servlet and parse json response. 在您的Android应用中,将buit httpclient中的http使用到远程servlet并解析json响应。 So. 所以。 Servlets,Httpclient,Json covers most if your app is native to phone app. 如果您的应用程序是电话应用程序的本机,则Servlets,Httpclient,Json涵盖了大多数内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 设置连接到数据库的服务器,从中获取信息,然后将其发送到 Android 应用程序 - Set up a server that connects to a database, obtains the information from it, then sends it to an Android application 在 Android 应用程序中进行远程服务器调用 - Making remote server calls in an Android application 通过HTTP连接到远程数据库的富客户端(Swing)应用程序 - Rich client (swing) application which connects to Remote database over http Android连接到Java服务器套接字,但服务器套接字未接收数据 - Android connects to java server socket, but server socket is not receiving data 为连接到服务器应用程序的每个客户端打开新的数据库连接? - Opening a new database connection for every client that connects to the server application? 如果 Java 应用程序连接到 MQ 客户端,而不是 MQ 服务器,是否可行? - Is it posible if a Java application connects to MQ Client, not MQ Server? Android tcp客户端连接然后从Java服务器断开连接 - Android tcp client connects then disconnect from java server 我需要在连接到服务器的另一个Android活动中使用线程 - I need to use a thread in another android activity that connects to a server android远程服务器 - android remote server 带有远程服务器的Android客户端 - Android client with remote server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM