我的應用程序包含幾個按鈕,我想要的是通過單擊任何按鈕,它將引導我到一個URL,然後帶我到一個JSON對象頁面,然後爲我提供一個圖像源顯示在我的Android設備上。從服務器使用JSON的Android下載圖像
例如:按鈕1 - >http://a.b.c.d/loadview.htm?buttonid=B1 - > Json對象(img src- url到圖像文件) - >顯示在我的android設備上。
感謝提前:)
我的應用程序包含幾個按鈕,我想要的是通過單擊任何按鈕,它將引導我到一個URL,然後帶我到一個JSON對象頁面,然後爲我提供一個圖像源顯示在我的Android設備上。從服務器使用JSON的Android下載圖像
例如:按鈕1 - >http://a.b.c.d/loadview.htm?buttonid=B1 - > Json對象(img src- url到圖像文件) - >顯示在我的android設備上。
感謝提前:)
這是工作代碼:從服務器下載圖像和Android的
public void onCreate(Bundle savedInstanceState)
{
Bitmap bitmap = DownloadImage("http://www.aaa.com/images//29_13.jpeg");
ImageView img = (ImageView) findViewById(R.id.imagefromserver);
img.setImageBitmap(bitmap);
}
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
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;
}
private Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
你們的榜樣網址不工作,但我會盡量嘗試解釋:
WebRequest request = HttpWebRequest.Create("test.aspx");
WebResponse response = request.GetResponse();
using (StreamReader streamReader = new StreamReader(response.GetResponseStream()))
{
string json = streamReader.ReadToEnd();
JavaScriptSerializer ser = new JavaScriptSerializer();
var deserializedObject = ser.Deserialize(json, typeof(ControllerBuilder));
if(deserializedObject != null)
//cast object to the correct type and use it.
}
我不建議使用內置的JavaScriptSerializer類,只是因爲我有性能問題。使用更好的工具是:
http://james.newtonking.com/pages/json-net.aspx
希望這可以幫助你!
You need to open a HttpConnection with the url to image source and download it.
Here is the code:
Bitmap getTheBitmap(String yourImgSrcUrl)
{
URL url = null;
Bitmap bitmap = null;
DataInputStream fileInputStream = null;
try
{
String urlPath = yourImgSrcUrl;
urlPath = urlPath.replace(" ", "%20"); // to replace any blank spaces
url = new URL(urlPath);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setUseCaches(false);
connection.setRequestMethod("GET");
fileInputStream = new DataInputStream(connection.getInputStream());
byte[] bitmapBytes= new byte[fileInputStream.available()];
fileInputStream.read(bitmapBytes);
fileInputStream.close();
bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length);
return bitmap;
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
}
Kindly check that Bitmap you are getting is not null before setting in the ImageView else it will throw exception.
喜顯示它我得到的位圖作爲空。可能是什麼問題。 – AndroidRaji 2012-12-07 05:31:54