0

I am getting the following error java.net.MalformedURLException in Logcat when trying to execute my String that contains my URL with Asynctask. I know the string contains the correct URL because I am using it to load an image into an ImageView via Picasso Library(See below).

Any help would be appreciated, thanks. Async Task class:

public class SetWallpaperAsync extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
String image_url;
URL ImageUrl;
String myFileUrl1;
Bitmap bmImg = null;

public SetWallpaperAsync(Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub

    super.onPreExecute();

    pDialog = new ProgressDialog(context);
    pDialog.setMessage("Please wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();

}

@Override
protected String doInBackground(String... args) {
    // TODO Auto-generated method stub

    InputStream is = null;

    try {

        ImageUrl = new URL(args[0]);
        // myFileUrl1 = args[0];

        HttpURLConnection conn = (HttpURLConnection) ImageUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Config.RGB_565;
        bmImg = BitmapFactory.decodeStream(is, null, options);

    } catch (IOException e) {
        e.printStackTrace();
    }

    finally {
        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

    return null;
}

@Override
protected void onPostExecute(String args) {
    // TODO Auto-generated method stub

    if (bmImg == null) {

        Toast.makeText(context, "Image still loading...",
                Toast.LENGTH_SHORT).show();

        pDialog.dismiss();

    }

    else {

        WallpaperManager wpm = WallpaperManager.getInstance(context);
        try {
            wpm.setBitmap(bmImg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (pDialog != null) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
        }

        Toast.makeText(context, "Wallpaper Successfully Set!",
                Toast.LENGTH_SHORT).show();

    }
}
}

What is says it Logcat once called:

     04-15 13:42:57.764: W/System.err(5753): java.net.MalformedURLException
04-15 13:42:57.764: W/System.err(5753):     at java.net.URL.<init>(URL.java:154)
04-15 13:42:57.764: W/System.err(5753):     at java.net.URL.<init>(URL.java:127)
04-15 13:42:57.768: W/System.err(5753):     at com.jaypps.SetWallpaperAsync.doInBackground(SetWallpaperAsync.java:58)
04-15 13:42:57.772: W/System.err(5753):     at com.jaypps.SetWallpaperAsync.doInBackground(SetWallpaperAsync.java:1)
04-15 13:42:57.772: W/System.err(5753):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-15 13:42:57.772: W/System.err(5753):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-15 13:42:57.772: W/System.err(5753):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-15 13:42:57.776: W/System.err(5753):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-15 13:42:57.776: W/System.err(5753):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-15 13:42:57.776: W/System.err(5753):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-15 13:42:57.776: W/System.err(5753):     at java.lang.Thread.run(Thread.java:856)
04-15 13:42:57.792: W/EGL_emulation(5753): eglSurfaceAttrib not implemented

Also, here is how I am calling Async Task, Also, I know that the String I am passing it to set the wallpaper contains the correct URL because I am also passing the same String to load that image into an ImageView using the Picasso Library:

public class ImageViewer extends SherlockFragment {

private String ImageURL;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.imageviewpager, container, false);

    ImageView IMGView = (ImageView) view.findViewById(R.id.imageView1);

    Picasso.with(getActivity()).load(getArguments().getString(ImageURL))
            .into(IMGView);

    IMGView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub



            new SetWallpaperAsync(getActivity()).execute(ImageURL);

        }
    });

    return view;
}

 }
4

2 回答 2

0

好吧,您肯定没有使用 URL。使用调试器检查该字符串的实际内容。您也根本没有初始化 url 变量。

于 2014-04-15T14:00:17.360 回答
0

Make sure ImageUrl is a valid http url http://developer.android.com/reference/java/net/MalformedURLException.html

于 2014-04-15T14:03:54.390 回答