运行结果
MainActivity.java
package com.imageonline;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.imageonline.utils.HttpUtils;
import com.imageonline.utils.NetWorkUtils;
public class MainActivity extends Activity
{
public EditText edtext;
public ImageView image;
public NetWorkUtils netWorkUtils;
private Handler handler;
public Bitmap bm;
public static final int TEXT = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
edtext = (EditText) findViewById(R.id.editText1);
image = (ImageView) findViewById(R.id.imageView1);
netWorkUtils = new NetWorkUtils(this);
handler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
switch (msg.what)
{
case TEXT:
image.setImageBitmap(bm);// 设置显示的文本
break;
default:
break;
}
}
};
}
public 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 sendHttp(View v){
boolean flag = netWorkUtils.setActiveNetWork();
if(flag){
final String path = edtext.getText().toString();
if(TextUtils.isEmpty(path)){
Toast.makeText(MainActivity.this, "访问的url地址不能为空", Toast.LENGTH_LONG).show();
}else{
new Thread(new Runnable() {
@Override
public void run() {
bm = HttpUtils.sendGets(path);
handler.sendEmptyMessage(TEXT);
}
}).start();
}
}
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/editText1"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_launcher" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="96dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="35dp"
android:onClick="sendHttp"
android:text="search" />
</RelativeLayout>
<uses-permission
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.INTERNET"/>
android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission
android:name="android.permission.INTERNET"/>