一步一步学会http获取tomcat服务端的图片,在android客户端显示

package com.example.animal;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class HttpGetActivity extends Activity {ImageView img;Button btn, save;URL url;Bitmap bitmap;HttpURLConnection httpURLConnection;InputStream is;FileOutputStream fos;Runnable run = new Runnable() {@Overridepublic void run() {String urlname = ":8080/Test/test.jpg";try {url = new URL(urlname);httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(3000);//允许输入流httpURLConnection.setDoInput(true);//设置连接方式为GET,,默认就是get请求方式httpURLConnection.setRequestMethod("GET");int responseCode = httpURLConnection.getResponseCode();// 状态码等于200,表示正确if (responseCode == 200) {is = httpURLConnection.getInputStream();}bitmap = BitmapFactory.decodeStream(is);Log.i("测试", bitmap + "");handler.sendEmptyMessage(0);} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}};Runnable run2 = new Runnable() {@Overridepublic void run() {String urlname = ":8080/Test/test.jpg";try {url = new URL(urlname);httpURLConnection = (HttpURLConnection) url.openConnection();httpURLConnection.setConnectTimeout(3000);httpURLConnection.setDoInput(true);httpURLConnection.setRequestMethod("GET");int responseCode = httpURLConnection.getResponseCode();// 状态码等于200,表示正确if (responseCode == 200) {is = httpURLConnection.getInputStream();}File file=new File("sdcard/test/");file.mkdirs();fos = new FileOutputStream("sdcard/test/test.jpg");byte[] date = new byte[1024];int len;while ((len = is.read(date)) != -1) {fos.write(date, 0, len);}} catch (Exception e) {e.printStackTrace();} finally {if (is != null) {try {is.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}if (fos != null) {try {fos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}};StringBuffer sb;Handler handler = new Handler() {public void handleMessage(android.os.Message msg) {img.setImageBitmap(bitmap);};};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);img = (ImageView) findViewById(R.id.frame_image);btn = (Button) findViewById(R.id.run);save = (Button) findViewById(R.id.save);btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {new Thread(run).start();}});save.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread(run2).start();}});}}

没有口水与汗水,就没有成功的泪水。

一步一步学会http获取tomcat服务端的图片,在android客户端显示

相关文章:

你感兴趣的文章:

标签云: