使用 Volley 时出现格式错误的 URL 异常

Rev*_* V. 0 android malformedurlexception android-volley

在 android 中使用 Android-Volley 时出现以下错误。同样的问题在这里也仍未解决......

\n\n
08-27 14:46:18.779  26990-26990/com.rev.volleydemo D/VOLLEY\xef\xb9\x95 http://api.androidhive.info/volley/person_object.json\n08-27 14:46:18.879  26990-27004/com.rev.volleydemo E/Volley\xef\xb9\x95 [619] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL null\n    java.lang.RuntimeException: Bad URL null\n            at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)\n            at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)\n     Caused by: java.net.MalformedURLException\n            at java.net.URL.<init>(URL.java:154)\n            at java.net.URL.<init>(URL.java:127)\n            at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)\n            at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n
public class AppController extends Application {\n\n    public static final String TAG = AppController.class\n            .getSimpleName();\n\n    private RequestQueue mRequestQueue;\n    private ImageLoader mImageLoader;\n\n    private static AppController mInstance;\n\n    @Override\n    public void onCreate() {\n        super.onCreate();\n        mInstance = this;\n    }\n\n    public static synchronized AppController getInstance() {\n        return mInstance;\n    }\n\n    public RequestQueue getRequestQueue() {\n        if (mRequestQueue == null) {\n            mRequestQueue = Volley.newRequestQueue(getApplicationContext());\n        }\n\n        return mRequestQueue;\n    }\n\n    public ImageLoader getImageLoader() {\n        getRequestQueue();\n        if (mImageLoader == null) {\n            mImageLoader = new ImageLoader(this.mRequestQueue,\n                    new BitmapCache());\n        }\n        return this.mImageLoader;\n    }\n\n    public <T> void addToRequestQueue(Request<T> req, String tag) {\n        // set the default tag if tag is empty\n        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);\n        getRequestQueue().add(req);\n    }\n\n    public <T> void addToRequestQueue(Request<T> req) {\n        req.setTag(TAG);\n        getRequestQueue().add(req);\n    }\n\n    public void cancelPendingRequests(Object tag) {\n        if (mRequestQueue != null) {\n            mRequestQueue.cancelAll(tag);\n        }\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n
public class MainActivity extends AppCompatActivity {\n\n    public final String URL = new String("http://api.androidhive.info/volley/person_object.json");\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n        Log.d("VOLLEY", URL);\n\n        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, null, URL, new Response.Listener<JSONObject>() {\n            @Override\n            public void onResponse(JSONObject response) {\n                Log.d("VOLLEY", response.toString());\n            }\n        }, new Response.ErrorListener() {\n            @Override\n            public void onErrorResponse(VolleyError error) {\n                Log.d("VOLLEY", error.toString());\n            }\n        });\n\n        AppController.getInstance().addToRequestQueue(jsonObjectRequest, "JSON_OBJ");\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n

Tho*_*ben 5

错误就出在这里:

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, null, URL, new Response.Listener<JSONObject>() {
Run Code Online (Sandbox Code Playgroud)

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

请参阅此处:JsonObjectRequest

  • 尝试用“”(空字符串)替换 null ....现在它工作正常...这是正确的方法吗 (2认同)