Flickr网址上的java.net.MalformedURLException

时间:2015-10-30 20:51:16

标签: java android android-studio

运行用于从flickrs json feed中提取文本的代码时,我收到了MalformedURLException。

错误看起来像这样 -

Error
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData: java.net.MalformedURLException
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at java.net.URL.<init>(URL.java:152)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at java.net.URL.<init>(URL.java:125)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at com.example.harris.flickrbrowser.GetRawData$DownloadRawData.doInBackground(GetRawData.java:75)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at com.example.harris.flickrbrowser.GetRawData$DownloadRawData.doInBackground(GetRawData.java:50)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at android.os.AsyncTask$2.call(AsyncTask.java:295)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-30 20:41:43.149 6499-6522/com.example.harris.flickrbrowser E/GetRawData:     at java.lang.Thread.run(Thread.java:818)

MainActivity -

package com.example.harris.flickrbrowser;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);

        GetRawData theRawData = new GetRawData("http://api.flickr.com/services/feeds/photos_public.gne?format=json");
        theRawData.execute();


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

GetRawData -

package com.example.harris.flickrbrowser;

import android.os.AsyncTask;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by Harris on 10/30/2015.
 */
enum DownloadStatus {IDLE, PROCESSING, NOT_INITIALISED, FAILED, OK}
public class GetRawData {
    private String LOG_TAG = GetRawData.class.getSimpleName();
    private String mRawUrl;
    private String mData;
    private DownloadStatus mDownloadStatus;

    public GetRawData(String mRawData) {
        this.mRawUrl = mRawUrl;
        this.mDownloadStatus = DownloadStatus.IDLE;

    }

    public void reset() {
        this.mDownloadStatus = DownloadStatus.IDLE;
        this.mRawUrl = null;
        this.mData = null;
    }

    public DownloadStatus getmDownloadStatus() {
        return mDownloadStatus;
    }

    public String getmData() {
        return mData;
    }

    public void execute() {
        this.mDownloadStatus = DownloadStatus.PROCESSING;
        DownloadRawData downloadRawData = new DownloadRawData();
        downloadRawData.execute(mRawUrl);
    }

    public class DownloadRawData extends AsyncTask<String, Void, String> {
        protected void onPostExecute(String webData) {
            mData = webData;
            Log.v(LOG_TAG, "Data returned was" + mData);
            if (mData == null){
                if(mRawUrl == null){
                    mDownloadStatus = DownloadStatus.NOT_INITIALISED;
                }else {
                    mDownloadStatus = DownloadStatus.FAILED;
                }

            } else {
                mDownloadStatus = DownloadStatus.OK;
            }
        }


        protected String doInBackground(String... params) {
            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            if (params == null)
                return null;

            try {
                URL url = new URL(params[0]);

                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                InputStream inputStream = urlConnection.getInputStream();
                if (inputStream == null) {
                    return null;
                }
                StringBuffer buffer = new StringBuffer();

                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                while ((line = reader.readLine()) != null) {
                    buffer.append(line + "\n");
                }

                return buffer.toString();


            } catch (IOException e) {
                Log.e(LOG_TAG, "Error", e);
                return null;
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (final IOException e) {
                        Log.e(LOG_TAG, "Error closing stream", e);
                    }
                }

            }
        }

    }
}

我已经尝试过使用HTTP和HTTPS的URL本身的许多变体,并且所有变体都返回相同的错误。 为了它的价值我正在通过USB调试运行它到Nexus 5。

1 个答案:

答案 0 :(得分:0)

我认为你的构造函数中有一个错误:

public GetRawData(String mRawData) {
    //this should be this.mRawUrl = mRawData; or change your method signature naming
    this.mRawUrl = mRawUrl;

    this.mDownloadStatus = DownloadStatus.IDLE;

}