getInputStream()出了问题

package com.example.imagelooker;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownServiceException;

import org.apache.http.conn.ConnectTimeoutException;
import org.xml.sax.InputSource;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

private EditText et_path;
private ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_path = (EditText) findViewById(R.id.et_path);
    iv = (ImageView) findViewById(R.id.IV);
}
public void look(View v){
    String path=et_path.getText().toString();
    if(path.equals("")){
        Toast.makeText(getApplicationContext(), "路径为空", Toast.LENGTH_SHORT).show();
    }else {
        try {
            //构建路径
            URL url=new URL(path);
            //打开连接
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            //请求方式
            conn.setRequestMethod("GET");
            //超时时间
            conn.setConnectTimeout(5000);

// if(conn.getResponseCode()==200){
InputStream is = conn.getInputStream();

                //把流转化为图片
                Bitmap bitmap=BitmapFactory.decodeStream(is);
                if (bitmap==null) {
                    Toast.makeText(getApplicationContext(), "获得的图片流为空", Toast.LENGTH_SHORT).show();
                }else {
                    iv.setImageBitmap(bitmap);
                }
        /*  }else {
                Toast.makeText(getApplicationContext(), "输入的路径不存在", Toast.LENGTH_SHORT).show();
            }*/


        } catch (Exception e) {
            e.printStackTrace();
            if(e instanceof MalformedURLException){
                Toast.makeText(getApplicationContext(), "输入的路径格式异常", Toast.LENGTH_SHORT).show();
            }else if (e instanceof ConnectTimeoutException) {
                Toast.makeText(getApplicationContext(), "连接超时错误", Toast.LENGTH_SHORT).show();
            }else if (e instanceof IOException) {
                Toast.makeText(getApplicationContext(), "获取数据错误", Toast.LENGTH_SHORT).show();

            }else if (e instanceof UnknownServiceException) {
                Toast.makeText(getApplicationContext(), "协议不支持", Toast.LENGTH_SHORT).show();
            }
            else {
                Toast.makeText(getApplicationContext(), "未知错误", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

}


logcat 如下
图片说明

查看全部
qq_34563651
qq_34563651
2017/04/04 10:34
  • exception
  • 异常
  • 点赞
  • 收藏
  • 回答
    私信

1个回复