我正在尝试在Platform 2.2 Froyo上构建Android应用程序。应用程序应该连接到远程服务器,从中获取数据并以不同语言向用户显示。连接到远程服务器的Android应用程序
- 所以我的问题 - 我需要什么样的技术学习,这样我可以 建立上述应用。
注 - 我已经安装了Android平台,并且已经构建了像Hello,world这样的简单应用程序。我认识Java。另外我使用Eclipse。
谢谢你的回答。没有粗鲁的评论请...
// --------------代码连接到使用HTTP协议的网站--------------- - //
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);
}
}
谢谢。更具体地说....我想知道在哪里/如何开始开发一个“连接到网站并下载和显示数据的ANDROID APP”。“该网站是一个像天气网站,音乐网站或谷歌地图网站的正常网站,如你所说的,再次感谢你 – Niteesh 2012-01-18 06:53:49
相应地编辑了我的答案 – Martin 2012-01-30 17:07:00
Martin,谢谢你的回答,好吧,我试图连接到这个网站:http://www.kvkkolhapur.com/这显然不属于我,它是一个政府网站,所以我需要联系网站管理员以获取详细信息。正如您所说,我将首先使用Google Map例如,再次感谢您的答复,您的回答很有帮助 – Niteesh 2012-02-07 12:51:11