我的应用程序包含几个按钮,我想要的是通过单击任何按钮,它将引导我到一个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