当我尝试使用Volley从服务器加载图像时出现错误。
这是我的日志:
E/Volley: [31648] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/103.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/103.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/103.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31650] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/106.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/106.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/106.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31651] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/106.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/106.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/106.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31649] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/103.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/103.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/103.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31648] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/0.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/0.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/0.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31650] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/107.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/107.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/107.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31651] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/0.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/0.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/0.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
E/Volley: [31651] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/107.png
java.lang.RuntimeException: Bad URL kinandayu.com/image_content/uploads/107.png
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:144)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
Caused by: java.net.MalformedURLException: Protocol not found: kinandayu.com/image_content/uploads/107.png
at java.net.URL.<init>(URL.java:176)
at java.net.URL.<init>(URL.java:125)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:102)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
这是我的Activity.java:
public class Child_Tab1 extends Activity {
List<GetDataAdapter> GetDataAdapter1;
RecyclerView recyclerView;
RecyclerView.LayoutManager recyclerViewlayaoutManager;
RecyclerView.Adapter recyclerViewadapter;
String GET_JSON_DATA_HTTP_URL = "http://www.kinandayu.com/image_content/getAllImage.php";
String JSON_IMAGE_TITLE_NAME = "id";
String JSON_IMAGE_URL = "image";
JsonArrayRequest jsonArrayRequest;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.child_tab_1);
GetDataAdapter1 = new ArrayList<>();
recyclerView = (RecyclerView) findViewById(R.id.rv1);
recyclerView.setHasFixedSize(true);
recyclerViewlayaoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(recyclerViewlayaoutManager);
JSON_DATA_WEB_CALL();
}
private void JSON_DATA_WEB_CALL() {
jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse (JSONArray response) {
JSON_PARSE_DATA_AFTER_WEBCALL(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(this);
requestQueue.add(jsonArrayRequest);
}
public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array) {
for (int i = 0; i<array.length(); i++) {
GetDataAdapter GetDataAdapter2 = new GetDataAdapter();
JSONObject json = null;
try {
json = array.getJSONObject(i);
GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
} catch (JSONException e) {
e.printStackTrace();
}
GetDataAdapter1.add(GetDataAdapter2);
}
recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);
recyclerView.setAdapter(recyclerViewadapter);
}
}
这是我的ServerImageParseAdapter.java:
public class ServerImageParseAdapter {
public static ServerImageParseAdapter SIAdapter;
public static Context context1;
public RequestQueue requestQueue1;
public ImageLoader ImageLoader1;
public Cache cache1;
public Network networkOBJ;
LruCache<String, Bitmap> LRUCACHE = new LruCache<String, Bitmap>(30);
private ServerImageParseAdapter(Context context) {
this.context1 = context;
this.requestQueue1 = RQ();
ImageLoader1 = new ImageLoader(requestQueue1, new ImageLoader.ImageCache() {
@Override
public Bitmap getBitmap(String URL) {
return LRUCACHE.get(URL);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
LRUCACHE.put(url,bitmap);
}
});
}
public ImageLoader getImageLoader() {
return ImageLoader1;
}
public static ServerImageParseAdapter getInstance(Context SynchronizedContext) {
if (SIAdapter == null) {
SIAdapter = new ServerImageParseAdapter(SynchronizedContext);
}
return SIAdapter;
}
public RequestQueue RQ() {
if (requestQueue1 == null) {
cache1 = new DiskBasedCache(context1.getCacheDir());
networkOBJ = new BasicNetwork(new HurlStack());
requestQueue1 = new RequestQueue(cache1, networkOBJ);
requestQueue1.start();
}
return requestQueue1;
}
}
我正在使用编译'com.android.volley:volley:1.0.0'
问题是我的CardView中没有显示图像,但显示的是image("id")
的名称。
我已经阅读了一些关于同样问题的问题,但我没有得到任何解决方案。
答案 0 :(得分:2)
这是服务器端问题。您需要fix your PHP & JSON才能获得正确的网址
{
"id": "106",
"image": "kinandayu.com\/image_content\/uploads\/103.png"
}
如果您不能这样做,请将http://
添加到图片网址字符串中。
String imgURL = json.getString(JSON_IMAGE_URL);
if (!imgURL.startsWith("http://") {
imgURL = "http://"+imgURL;
}
GetDataAdapter2.setImageServerUrl(imgURL);