同时采用凌空畸形URL例外 [英] Malformed URL Exception while using Volley
本文介绍了同时采用凌空畸形URL例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
获取,而在Android中使用的是Android凌空下面的错误。同样的问题在这里仍然没有解决太...
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 /乱射:[619] NetworkDispatcher.run:未处理的异常了java.lang.RuntimeException:不良网址空
了java.lang.RuntimeException:错误的URL空
在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:151)
在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
java.net.MalformedURLException:产生的原因
在与的java.net.URL下。INIT>(URL.java:154)
在与的java.net.URL下。INIT>(URL.java:127)
在com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
在com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
在com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)
公共类AppController的扩展应用{ 公共静态最后的字符串标记= AppController.class
.getSimpleName(); 私人请求队列mRequestQueue;
私人ImageLoader的mImageLoader; 私有静态AppController的mInstance; @覆盖
公共无效的onCreate(){
super.onCreate();
mInstance =这一点;
} 公共静态同步的AppController的getInstance(){
返回mInstance;
} 公共请求队列getRequestQueue(){
如果(mRequestQueue == NULL){
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
} 返回mRequestQueue;
} 公共ImageLoader的getImageLoader(){
getRequestQueue();
如果(mImageLoader == NULL){
mImageLoader =新ImageLoader的(this.mRequestQueue,
新BitmapCache());
}
返回this.mImageLoader;
} 公众< T>无效addToRequestQueue(请求< T> REQ,字符串变量){
//设置默认如果tag是空的
req.setTag(?TextUtils.isEmpty(标签)标签:标签);
。getRequestQueue()加(REQ);
} 公众< T>无效addToRequestQueue(请求< T> REQ){
req.setTag(TAG);
。getRequestQueue()加(REQ);
} 公共无效cancelPendingRequests(对象标签){
如果(mRequestQueue!= NULL){
mRequestQueue.cancelAll(标签);
}
}
}
公共类MainActivity扩展AppCompatActivity { 公众最终字符串URL =新的String(http://api.androidhive.info/volley/person_object.json); @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
Log.d(VOLLEY,URL); JsonObjectRequest jsonObjectRequest =新JsonObjectRequest(Method.POST,空,网址,新Response.Listener<&JSONObject的GT;(){
@覆盖
公共无效onResponse(JSONObject的响应){
Log.d(VOLLEY,response.toString());
}
},新Response.ErrorListener(){
@覆盖
公共无效onErrorResponse(VolleyError错误){
Log.d(VOLLEY,error.toString());
}
}); 。AppController.getInstance()addToRequestQueue(jsonObjectRequestJSON_OBJ);
}
}
解决方案
错误就出在这里:
JsonObjectRequest jsonObjectRequest =新JsonObjectRequest(Method.POST,空,网址,新Response.Listener<&JSONObject的GT;(){
构造函数的第二个参数应该是URL,而不是第三个。
在这里看到: JsonObjectRequest
Getting the Error below while using Android-Volley in android. The same problem remains unsolved here too...
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");
}
}
解决方案
The error lies here:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Method.POST, null, URL, new Response.Listener<JSONObject>() {
The second parameter of the constructor should be the URL, not the third one.
See here: JsonObjectRequest
这篇关于同时采用凌空畸形URL例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文