我创建了这个方法,如果URL有效,则首先检查,然后检查它是否链接到图像或是否是YouTube视频。我的程序到达
时失败String contentType = connection.getHeaderField("Content-Type");
方法的一部分。为什么会这样?
此外,使用URLUtil.isValidUrl()检查其是否为有效URL以及创建连接并尝试打开该连接是无关紧要的。
private boolean checkContent() {
//check if valid image
boolean img = false;
boolean youtube = false;
if(URLUtil.isValidUrl(imageAddress)) {
URLConnection connection = null;
try {
connection = new URL(imageAddress).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
String contentType = connection.getHeaderField("Content-Type");
img = contentType.startsWith("image/");
if(!img) {
// Check host of url if youtube exists
Uri uri = Uri.parse(imageAddress);
if ("www.youtube.com".equals(uri.getHost()))
youtube = true;
}
}
return img || youtube;
}
我的Manifest中有互联网权限!!!
Process: com.example.robert.demo, PID: 11504
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.android.okhttp.HostResolver$1.getAllByName(HostResolver.java:29)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:232)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:124)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:272)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:211)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:382)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:332)
at com.android.okhttp.internal.http.HttpURLConnectionImpl.getHeaderField(HttpURLConnectionImpl.java:161)
at com.example.robert.demo.Demo.checkContent(Demo.java:110)
at com.example.robert.demo.Demo.submitFile(Demo.java:89)
at com.example.robert.demo.Demo.submitClick(Demo.java:65)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
使用Map<String, List<String>>
获取标题文件。试试这个:
private boolean checkContent() {
//check if valid image
boolean img = false;
boolean youtube = false;
if(URLUtil.isValidUrl(imageAddress)) {
URLConnection connection = null;
try {
connection = new URL(imageAddress).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
Map<String, List<String>> map = conn.getHeaderFields();
//This area shows keys and values for headerfields in logcat.
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
Log.d("Keysss", "Key : " + entry.getKey() +
" ,Value : " + entry.getValue());
}
//get header by 'key'
String contentType= connection.getHeaderField("Content-Type");
img = contentType.startsWith("image/");
if(!img) {
// Check host of url if youtube exists
Uri uri = Uri.parse(imageAddress);
if ("www.youtube.com".equals(uri.getHost()))
youtube = true;
}
}
return img || youtube;
}
答案 1 :(得分:0)
1)引起:android.os.NetworkOnMainThreadException at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) 在java.net.InetAddress.lookupHostByName(InetAddress.java:418)at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
尝试在单独的线程上运行Web方法代码,如下所示:
if(URLUtil.isValidUrl(imageAddress)) {
Thread thread = new Thread() {
@Override
public void run() {
URLConnection connection = null;
try {
connection = new URL(imageAddress).openConnection();
} catch (IOException e) {
e.printStackTrace();
}
String contentType = connection.getHeaderField("Content-Type");
img = contentType.startsWith("image/");
if(!img) {
// Check host of url if youtube exists
Uri uri = Uri.parse(imageAddress);
if ("www.youtube.com".equals(uri.getHost()))
youtube = true;
}
}
};
thread.start();
}