Ich versuche, das Facebook Profilbild mit dem Code unten zu erhalten. Ich versuche den Link, der im Protokoll ist, scheint in Ordnung, aber in der getContent() Teil bekomme ich den Fehler. Irgendwelche Ideen darüber, warum das passiert?Fehler beim Erhalten von Facebook Profilbild in Android
Mein Code ist:
imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
System.out.println("problem 7");
System.out.println(imgUrl);
InputStream in = (InputStream) imgUrl.getContent();
System.out.println("problem 7.5");
Bitmap bitmap = BitmapFactory.decodeStream(in);
System.out.println("problem 8");
Der Fehler ist wie folgt:
04-21 15:59:58.472 18711-18711/com.my_last.mylast I/System.out: problem 7
04-21 15:59:58.472 18711-18711/com.my_last.mylast I/System.out: https://graph.facebook.com/135258820209927/picture?type=large
04-21 15:59:58.481 18711-18711/com.my_last.mylast D/AndroidRuntime: Shutting down VM
04-21 15:59:58.481 18711-18711/com.my_last.mylast W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb309c180)
04-21 15:59:58.481 18711-18711/com.my_last.mylast E/AndroidRuntime: FATAL EXCEPTION: main
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
at java.net.InetAddress.getAllByName(InetAddress.java:220)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:460)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:432)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at java.net.URLConnection.getContent(URLConnection.java:194)
at libcore.net.http.HttpsURLConnectionImpl.getContent(HttpsURLConnectionImpl.java:174)
at java.net.URL.getContent(URL.java:447)
at com.my_last.mylast.signIn$3$2.onCompleted(signIn.java:365)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
04-21 15:59:58.992 18711-18713/com.my_last.mylast I/dalvikvm: threadid=3: reacting to signal 3
Edit:
ich meinen Code wie unten aktualisiert, und jetzt immer Fehler wie folgt:
Thread thread = new Thread() {
public void run() {
Message message = new Message();
try {
imgUrl = new URL("https://graph.facebook.com/" + facebookID + "/picture?type=large");
System.out.println("problem 7");
System.out.println(imgUrl);
Bitmap bitmap = BitmapFactory.decodeStream(imgUrl.openConnection().getInputStream());
System.out.println("problem 8");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArray1 = stream.toByteArray();
image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg");
image2 = new ParseFile("profilePhoto2.jpg", byteArray1, "image/jpg");
} catch (Exception e) {
Log.e("MYAPP", "unexpected JSON exception", e);
}
}
};
thread.start();
Fehler m esage:
04-21 16:40:29.118 10885-12039/com.my_last.mylast I/System.out: problem 7
04-21 16:40:29.118 10885-12039/com.my_last.mylast I/System.out: https://graph.facebook.com/135258820209927/picture?type=large
04-21 16:40:29.118 10885-10885/com.my_last.mylast D/AndroidRuntime: Shutting down VM
04-21 16:40:29.118 10885-10885/com.my_last.mylast W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb309c180)
04-21 16:40:29.118 10885-10885/com.my_last.mylast E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalArgumentException: value may not be null.
at com.parse.ParseObject.performPut(ParseObject.java:2860)
at com.parse.ParseObject.put(ParseObject.java:2851)
at com.my_last.mylast.signIn$3$2.onCompleted(signIn.java:406)
at com.facebook.GraphRequest$1.onCompleted(GraphRequest.java:304)
at com.facebook.GraphRequest$5.run(GraphRequest.java:1379)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
04-21 16:40:29.648 10885-10892/com.my_last.mylast I/dalvikvm: threadid=3: reacting to signal 3
04-21 16:40:29.658 10885-10892/com.my_last.mylast I/dalvikvm: Wrote stack traces to '/data/anr/traces.txt'
04-21 16:40:29.777 10885-12039/com.my_last.mylast I/System.out: problem 8
Sie sollten alle Netzwerkaufgaben im Hintergrund-Thread tun –
Ihr Betrieb auf Hauptthread ausführt. Sie sollten _Asynctask_ oder Thread verwenden, um mit diesem Fehler zu umgehen. – Piyush
Ich konnte es nicht bekommen Ich bin ein bisschen neu in Android. Könnten Sie bitte mehr Details geben? – saner