android中的http协议编程疑问

这个项目主要就是利用http协议从本地的web工程下下载一张图片,但是就是不能实现,求各位大神给予帮助!

代码如下:本地ip是10.110.110.212

一:MainActivity

packagecom.example.android_http_iamgeview;

importjava.io.IOError;

importjava.io.IOException;

importjava.io.InputStream;

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapFactory;

importandroid.view.Menu;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.ImageView;

publicclassMainActivityextendsActivity{

privateButtonbutton;

privateImageViewimageView;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button=(Button)findViewById(R.id.button);

imageView=(ImageView)findViewById(R.id.imageview);

button.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewarg0){

try{

InputStreaminputStream=HttpUtils

.getImageViewInputStream();

Bitmapbitmap=BitmapFactory.decodeStream(inputStream);

imageView.setImageBitmap(bitmap);

}catch(IOExceptione){

e.printStackTrace();

}

}

});

}

}

二:HttpUtils

packagecom.example.android_http_iamgeview;

importjava.io.IOException;

importjava.io.InputStream;

importjava.net.URL;

importjavax.net.ssl.HttpsURLConnection;

publicclassHttpUtils{

privatefinalstaticStringURL_PATH="http://10.110.110.212/myhttp/dongshu.png";

publicHttpUtils(){

}

publicstaticInputStreamgetImageViewInputStream()throwsIOException{

InputStreaminputStream=null;

URLurl=newURL(URL_PATH);

if(url!=null){

HttpsURLConnectionhttpsURLConnection=(HttpsURLConnection)url.openConnection();

httpsURLConnection.setConnectTimeout(3000);

httpsURLConnection.setRequestMethod("GET");

httpsURLConnection.setDoInput(true);

intresponse_code=httpsURLConnection.getResponseCode();

if(response_code==200){

inputStream=httpsURLConnection.getInputStream();

}

}

returninputStream;

}

}

三:activity_main.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical">

<ImageView

android:id="@+id/imageview"

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<Button

android:id="@+id/button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="从网络下载图片"/>

</LinearLayout>

将apk安装到手机上(不是模拟器),出现异常,求解决!!!

相关推荐