MianActivity.java
public class MainActivity extends AppCompatActivity {
button visitWebBtn = null;
button downimgBtn = null;
TextVIEw showtextVIEw = null;
ImageVIEw showImageVIEw = null;
String resultStr = “”;
Progressbar progressbar = null;
VIEwGroup vIEwGroup = null;
@OverrIDe
protected voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.activity_main);
initUI();
visitWebBtn.setonClickListener(new VIEw.OnClickListener(){
@OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub showImageVIEw.setVisibility(VIEw.GONE); showtextVIEw.setVisibility(VIEw.VISIBLE); Thread visitBaIDuThread = new Thread(new VisitWebRunnable()); visitBaIDuThread.start(); try { visitBaIDuThread.join(); if(!resultStr.equals("")){ showtextVIEw.setText(resultStr); } } catch (InterruptedException e) { // Todo auto-generated catch block e.printstacktrace(); } } }); downimgBtn.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { // Todo auto-generated method stub showImageVIEw.setVisibility(VIEw.VISIBLE); showtextVIEw.setVisibility(VIEw.GONE); String imgurl = "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=112912491,3646496790&fm=26&gp=0.jpg"; new DownimgAsyncTask().execute(imgurl); } });}@OverrIDepublic boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); return true;}public voID initUI(){ showtextVIEw = (TextVIEw)findVIEwByID(R.ID.textvIEw_show); showImageVIEw = (ImageVIEw)findVIEwByID(R.ID.imagvIEw_show); downimgBtn = (button)findVIEwByID(R.ID.btn_download_img); visitWebBtn = (button)findVIEwByID(R.ID.btn_visit_web);}/** * 获取指定URL的响应字符串 * @param urlString * @return */private String getURLResponse(String urlString){ httpURLConnection conn = null; //连接对象 inputStream is = null; String resultData = ""; try { URL url = new URL(urlString); //URL对象 conn = (httpURLConnection)url.openConnection(); //使用URL打开一个链接 conn.setDoinput(true); //允许输入流,即允许下载 conn.setDoOutput(true); //允许输出流,即允许上传 conn.setUseCaches(false); //不使用缓冲 conn.setRequestMethod("GET"); //使用get请求 is = conn.getinputStream(); //获取输入流,此时才真正建立链接 inputStreamReader isr = new inputStreamReader(is); BufferedReader bufferReader = new BufferedReader(isr); String inputline = ""; while((inputline = bufferReader.readline()) != null){ resultData += inputline + "\n"; } } catch (MalformedURLException e) { // Todo auto-generated catch block e.printstacktrace(); }catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); }finally{ if(is != null){ try { is.close(); } catch (IOException e) { // Todo auto-generated catch block e.printstacktrace(); } } if(conn != null){ conn.disconnect(); } } return resultData;}/** * 从指定URL获取图片 * @param url * @return */private Bitmap getimageBitmap(String url){ URL imgurl = null; Bitmap bitmap = null; try { imgurl = new URL(url); httpURLConnection conn = (httpURLConnection)imgurl.openConnection(); conn.setDoinput(true); conn.connect(); inputStream is = conn.getinputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (MalformedURLException e) { // Todo auto-generated catch block e.printstacktrace(); }catch(IOException e){ e.printstacktrace(); } return bitmap;}class VisitWebRunnable implements Runnable{ @OverrIDe public voID run() { // Todo auto-generated method stub String data = getURLResponse("http://www.baIDu.com/"); resultStr = data; }}class DownimgAsyncTask extends AsyncTask<String, VoID, Bitmap> { @OverrIDe protected voID onPreExecute() { // Todo auto-generated method stub super.onPreExecute(); showImageVIEw.setimageBitmap(null); showProgressbar();//显示进度条提示框 } @OverrIDe protected Bitmap doInBackground(String... params) { // Todo auto-generated method stub Bitmap b = getimageBitmap(params[0]); return b; } @OverrIDe protected voID onPostExecute(Bitmap result) { // Todo auto-generated method stub super.onPostExecute(result); if(result!=null){ dismissprogressbar(); showImageVIEw.setimageBitmap(result); } }}/** * 在母布局中间显示进度条 */private voID showProgressbar(){ progressbar = new Progressbar(this, null, androID.R.attr.progressbarStyleLarge); relativeLayout.LayoutParams params = new relativeLayout.LayoutParams(VIEwGroup.LayoutParams.WRAP_CONTENT, VIEwGroup.LayoutParams.WRAP_CONTENT); params.addRule(relativeLayout.CENTER_IN_PARENT, relativeLayout.TRUE); progressbar.setVisibility(VIEw.VISIBLE); Context context = getApplicationContext(); vIEwGroup = (VIEwGroup)findVIEwByID(R.ID.parent_vIEw); // MainActivity.this.addContentVIEw(progressbar, params); vIEwGroup.addVIEw(progressbar, params);}/** * 隐藏进度条 */private voID dismissprogressbar(){ if(progressbar != null){ progressbar.setVisibility(VIEw.GONE); vIEwGroup.removeVIEw(progressbar); progressbar = null; }}
}
activity_main.xml
<FrameLayout androID:layout_wIDth="match_parent" androID:layout_height="match_parent" > <TextVIEw androID:ID="@+ID/textvIEw_show" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="hello_world" /> <ImageVIEw androID:ID="@+ID/imagvIEw_show" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_gravity="center" /> </FrameLayout> <button androID:ID="@+ID/btn_visit_web" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:layout_alignParentleft="true" androID:text="访问百度" /> <button androID:ID="@+ID/btn_download_img" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true" androID:layout_toRightOf="@ID/btn_visit_web" androID:text="下载图片"/></relativeLayout>
总结 以上是内存溢出为你收集整理的Android网络实例:HTTP之利用HttpURLConnection访问百度、获取网络图片全部内容,希望文章能够帮你解决Android网络实例:HTTP之利用HttpURLConnection访问百度、获取网络图片所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)