【问题标题】:Malformed URL Exception while using Volley使用 Volley 时出现格式错误的 URL 异常
【发布时间】:2015-08-27 09:23:03
【问题描述】:

在 android 中使用 Android-Volley 时出现以下错误。同样的问题也没有解决here...

08-27 14:46:18.779  26990-26990/com.rev.volleydemo D/VOLLEY﹕ http://api.androidhive.info/volley/person_object.json
08-27 14:46:18.879  26990-27004/com.rev.volleydemo E/Volley﹕ [619] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL null
    java.lang.RuntimeException: Bad URL null
            at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
            at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
     Caused by: java.net.MalformedURLException
            at java.net.URL.<init>(URL.java:154)
            at java.net.URL.<init>(URL.java:127)
            at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
            at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
            at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)

public class AppController extends Application {

    public static final String TAG = AppController.class
            .getSimpleName();

    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new BitmapCache());
        }
        return this.mImageLoader;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

public class MainActivity extends AppCompatActivity {

    public final String URL = new String("http://api.androidhive.info/volley/person_object.json");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d("VOLLEY", URL);

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, null, URL, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Log.d("VOLLEY", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("VOLLEY", error.toString());
            }
        });

        AppController.getInstance().addToRequestQueue(jsonObjectRequest, "JSON_OBJ");
    }
}

【问题讨论】:

    标签: android android-volley malformedurlexception


    【解决方案1】:

    错误就在这里:

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, null, URL, new Response.Listener<JSONObject>() {
    

    构造函数的第二个参数应该是URL,而不是第三个。

    请看这里:JsonObjectRequest

    【讨论】:

    • 试过了 => JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, URL, null, listener, errorListener);但它说无法解析构造函数
    • 嗨!使用哪个 Volley 库? Jar 文件或在 build.gradle 中编译?它们之间存在一些差异。
    • 尝试用“”(空字符串)替换 null ......现在它工作正常......这是正确的做法
    • 使用 gradle compile 'com.mcxiaoke.volley:library:1.0.18'
    • 谢谢@Thorben @ BNK :)
    猜你喜欢
    • 2015-09-04
    • 2020-08-06
    • 2017-01-01
    • 1970-01-01
    • 2014-08-16
    • 2014-07-19
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    相关资源
    最近更新 更多