我正在嘗試在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