java.net.MalformedURLException:找不到协议

时间:2018-05-30 06:18:20

标签: android url network-protocols malformedurlexception

我正在开发<新闻应用程序和我需要在Textview中一致地显示图像和文本。 This is我希望在应用中显示的确切消息。

所以我写了以下代码:

public class NewsActivity extends AppCompatActivity implements FeedAdapter.OnItemClickListener, Html.ImageGetter
{

TextView textViewBody;

@Override
protected void onCreate(Bundle savedInstanceState)
{ 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_news);

    textViewBody = findViewById(R.id.textview_newsBody);
    Intent intent = getIntent();
    String body = intent.getStringExtra(EXTRA_BODY);
    body = body.replace("&lt;", "<").replace("&gt;", ">");
    Spanned spanned = Html.fromHtml(body, this, null);
    textViewBody.setText(spanned);
}

@Override
public Drawable getDrawable(String source)
{
    LevelListDrawable drawable = new LevelListDrawable();
    Drawable empty = getResources().getDrawable(R.drawable.ic_arxiv);
    drawable.addLevel(0, 0, empty);
    drawable.setBounds(0,0,empty.getIntrinsicWidth(), empty.getIntrinsicHeight());

    new LoadImage().execute(source, drawable);
    return drawable;
}

class LoadImage extends AsyncTask<Object, Void, Bitmap>
{
    private LevelListDrawable mDrawable;

    @Override
    protected Bitmap doInBackground(Object... params) {
        String source = (String) params[0];
        mDrawable = (LevelListDrawable) params[1];
        Log.d(TAG, "doInBackground " + source);
        try {
            InputStream is = new URL(source).openStream();
            return BitmapFactory.decodeStream(is);
        }  catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) 
    {
        Log.d(TAG, "onPostExecute drawable " + mDrawable);
        Log.d(TAG, "onPostExecute bitmap " + bitmap);
        if (bitmap != null) {
            BitmapDrawable d = new BitmapDrawable(getApplicationContext().getResources(), bitmap);
            mDrawable.addLevel(1, 1, d);
            mDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
            mDrawable.setLevel(1);
            // i don't know yet a better way to refresh TextView
            // mTv.invalidate() doesn't work as expected
            CharSequence t = textViewBody.getText();
            textViewBody.setText(t);
            textViewBody.refreshDrawableState();
        }
    }
}

我在尝试打开此新闻时为每张图片收到MalformedURLException:

W/System.err: java.net.MalformedURLException: Protocol not found: "https://metbuat.az/uploads/295/7f54088035-img6105.jpg"

如果我将URL复制粘贴到浏览器就可以了,并且已经提供了协议。

为什么我仍然得到这个例外?

2 个答案:

答案 0 :(得分:1)

因此改变了这一行:

InputStream is = new URL(source).openStream();

InputStream is = new URL(source.replace("&quot;", "\"")).openStream();

答案 1 :(得分:1)

使用glide esay将图像添加到依赖性以下的应用程序级别gradle文件中。

implementation 'com.github.bumptech.glide:glide:4.7.1'

然后制作这样的代码..

    // if activity then pass only this and fragment then pass getActivity()
    Glide.with(this)
            .load("https://metbuat.az/uploads/295/7f54088035-img6105.jpg")
            .into(imageview);