网络图片查看器:(读取网络图片,但是每次都是在执行.getResponseCode()方法的时候抛出异常)
找出问题点了,在MainActivity中调用这个类的网络操作方法,可能会导致activity的一些问题,谷歌从在android2.3版本以后,系统增加了一个类:StrictMode。这个类对网络的访问方式进行了一定的改变。
StrictMode通常用于捕获磁盘访问或者网络访问中与主进程之间交互产生的问题,因为在主进程中,UI操作和一些动作的执行是最经常用到的,它们之间会产生一定的冲突问题。将磁盘访问和网络访问从主线程中剥离可以使磁盘或者网络的访问更加流畅,提升响应度和用户体验。
如果一定要这样操作的话,需要在MainActivity中加入如下代码:
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build()); StrictMode.setVmPolicy( new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
并且在配置文件中将SDK的最低等级设为11
详细实现代码如下:
MainActivity.java类,实现主界面。有两个按钮分别对应不同的功能,一个按钮是通过内部类实现的点击事件处理,一个是通过配置设置的点击函数实现的事件处理。
package com.example.netimage; import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button=(Button) this.findViewById(R.id.imagebutton); button.setOnClickListener(new ButtonOnclickListener()); } public void openOterProcess(View v) { Log.i("testOpenOterProcess","the meth is Ok"); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); ComponentName cn = new ComponentName("dbSQLiteOPenHelper/dateoperate", "dateOperate"); intent.setComponent(cn); startActivity(intent); } private final class ButtonOnclickListener implements View.OnClickListener { @Override public void onClick(View v) { Intent intent=new Intent(); intent.setClass(MainActivity.this, NetImageActivity.class); startActivity(intent); } } @Override 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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }下面主要是显示图片的activity,有些Log.i()函数,是我用来测试发现问题用的。刚刚开始老会出现 在执行.getResponseCode()方法的时候抛出异常(已解决).
package com.example.netimage; import com.example.service.ImageService; import android.app.Activity; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.StrictMode; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class NetImageActivity extends Activity { private EditText pathText; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_netimage); //将磁盘访问和网络访问从主线程中剥离可以使磁盘或者网络的访问更加流畅,提升响应度和用户体验。 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy .Builder() .detectDiskReads() .detectDiskWrites() .detectNetwork() .penaltyLog().build()); StrictMode.setVmPolicy(new StrictMode .VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() .penaltyLog() .penaltyDeath() .build()); pathText = (EditText)this.findViewById(R.id.imagepath); imageView = (ImageView)this.findViewById(R.id.imageview); Button button=(Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonOnclickListener()); } private final class ButtonOnclickListener implements View.OnClickListener { @Override public void onClick(View v) { String path=pathText.getText().toString(); try { Log.i("testGetimage", "onClick正常"); byte[] data = ImageService.getImage(path); Log.i("testGetimage", "ImageService正常"); Bitmap bitmap=BitmapFactory.decodeByteArray(data, 0, data.length); imageView.setImageBitmap(bitmap); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), R.string.failed, 1).show(); } } } @Override 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; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }获取网络图片的数据。
package com.example.service; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.util.Log; import com.example.utils.StreamTool; public class ImageService { /* * 获取网络图片的数据 * @param path网络图片路径 * @return */ public static byte[] getImage(String path) throws Exception{ URL url=new URL(path); Log.i("testGetimage", url.toString()); HttpURLConnection conn=(HttpURLConnection)url.openConnection();//基于HTTP协议的链接对象 conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode()==200) { InputStream instream=conn.getInputStream(); Log.i("testGetimage", "IO正常"); return StreamTool.read(instream); }else{ Log.i("testGetimage", "IO异常"); } return null; } }imageService中的工具类实现。
package com.example.utils; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class StreamTool { public static byte[] read(InputStream instream) throws Exception{ ByteArrayOutputStream outStream=new ByteArrayOutputStream(); byte[] buffer=new byte[1024]; int len=0; while((len=instream.read(buffer))!=-1) { outStream.write(buffer,0,len); } instream.close(); return outStream.toByteArray(); } }布局设置(layout文件)
一:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/imagepath"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://10.0.159.3:8080/web/meinv2.jpg" android:id="@+id/imagepath"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageview" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/imagebutton" android:id="@+id/imagebutton"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/openotheractivtiybutton" android:id="@+id/openotheractivtiybutton" android:onClick="openOterProcess"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.netimage" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".NetImageActivity"> </activity> </application> <uses-permission android:name="android.permission.INTERNET"/> </manifest>网络源码查看器:
package com.example.service; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.util.Log; import com.example.utils.StreamTool; public class pageService { public static String getHtml(String path) throws Exception{ URL url=new URL(path); HttpURLConnection conn=(HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200) { InputStream in=conn.getInputStream(); byte[] data=StreamTool.read(in); return new String(data,"utf-8"); } return null; } }和网络图片查看器的实现类似,只是获得数据的格式有些不一样。
下面是查看源码的按钮处理事件函数:
public void htmlView(View v) { String path=pathText.getText().toString(); try{ String html=pageService.getHtml(path); codeText.setText(html); }catch(Exception e){ e.printStackTrace(); Toast.makeText(this.getApplicationContext(),R.string.gethtmlfaied,1).show(); } }经常会遇到 .getResponseCode()抛出异常或者无响应的情况,原因可能是网络问题,或者是这些操作应该放在子线程正实现。我碰到是通过上面方法解决的。有同样问题且上面方法解决不了的可以一起讨论。