【问题标题】:java.net.MalformedURLException: Protocol not foundjava.net.MalformedURLException:找不到协议
【发布时间】:2018-11-08 21:18:33
【问题描述】:

我正在开发 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 复制粘贴到浏览器,则可以,并且协议已经提供。

为什么我仍然收到此异常?

【问题讨论】:

  • 通过调试交叉检查 url 是否包含在引号中。其实不应该
  • 谢谢,我只需要这个:.replace(""", "\"");

标签: android url network-protocols malformedurlexception


【解决方案1】:

因此换行:

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

喜欢

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

【讨论】:

    【解决方案2】:

    使用 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);
    

    【讨论】:

      猜你喜欢
      • 2012-09-03
      • 1970-01-01
      • 2010-12-14
      • 2014-05-26
      • 1970-01-01
      • 2011-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多