Inspector crashes with NetworkOnMainThreadException in Android
Reviewed By: johnislarry Differential Revision: D5062290 fbshipit-source-id: 0307d2d950b6f54c36f882470c3ac4c5cfb1c5b7
This commit is contained in:
parent
49c4c0c942
commit
93f07ed31d
|
@ -10,6 +10,7 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
import android.os.AsyncTask;
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
|
|
||||||
|
@ -52,7 +53,7 @@ public class InspectorPackagerConnection {
|
||||||
try {
|
try {
|
||||||
JSONObject payload = makePageIdPayload(pageId);
|
JSONObject payload = makePageIdPayload(pageId);
|
||||||
sendEvent("open", payload);
|
sendEvent("open", payload);
|
||||||
} catch (JSONException | IOException e) {
|
} catch (JSONException e) {
|
||||||
FLog.e(TAG, "Failed to open page", e);
|
FLog.e(TAG, "Failed to open page", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -85,7 +86,7 @@ public class InspectorPackagerConnection {
|
||||||
mInspectorConnections.clear();
|
mInspectorConnections.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleConnect(JSONObject payload) throws JSONException, IOException {
|
private void handleConnect(JSONObject payload) throws JSONException {
|
||||||
final String pageId = payload.getString("pageId");
|
final String pageId = payload.getString("pageId");
|
||||||
Inspector.LocalConnection inspectorConnection = mInspectorConnections.remove(pageId);
|
Inspector.LocalConnection inspectorConnection = mInspectorConnections.remove(pageId);
|
||||||
if (inspectorConnection != null) {
|
if (inspectorConnection != null) {
|
||||||
|
@ -99,7 +100,7 @@ public class InspectorPackagerConnection {
|
||||||
public void onMessage(String message) {
|
public void onMessage(String message) {
|
||||||
try {
|
try {
|
||||||
sendWrappedEvent(pageId, message);
|
sendWrappedEvent(pageId, message);
|
||||||
} catch (IOException | JSONException e) {
|
} catch (JSONException e) {
|
||||||
FLog.w(TAG, "Couldn't send event to packager", e);
|
FLog.w(TAG, "Couldn't send event to packager", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -109,7 +110,7 @@ public class InspectorPackagerConnection {
|
||||||
try {
|
try {
|
||||||
mInspectorConnections.remove(pageId);
|
mInspectorConnections.remove(pageId);
|
||||||
sendEvent("disconnect", makePageIdPayload(pageId));
|
sendEvent("disconnect", makePageIdPayload(pageId));
|
||||||
} catch (IOException | JSONException e) {
|
} catch (JSONException e) {
|
||||||
FLog.w(TAG, "Couldn't send event to packager", e);
|
FLog.w(TAG, "Couldn't send event to packager", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,7 +132,7 @@ public class InspectorPackagerConnection {
|
||||||
inspectorConnection.disconnect();
|
inspectorConnection.disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void handleWrappedEvent(JSONObject payload) throws JSONException, IOException {
|
private void handleWrappedEvent(JSONObject payload) throws JSONException {
|
||||||
final String pageId = payload.getString("pageId");
|
final String pageId = payload.getString("pageId");
|
||||||
String wrappedEvent = payload.getString("wrappedEvent");
|
String wrappedEvent = payload.getString("wrappedEvent");
|
||||||
Inspector.LocalConnection inspectorConnection = mInspectorConnections.get(pageId);
|
Inspector.LocalConnection inspectorConnection = mInspectorConnections.get(pageId);
|
||||||
|
@ -153,7 +154,7 @@ public class InspectorPackagerConnection {
|
||||||
return array;
|
return array;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendWrappedEvent(String pageId, String message) throws IOException, JSONException {
|
private void sendWrappedEvent(String pageId, String message) throws JSONException {
|
||||||
JSONObject payload = new JSONObject();
|
JSONObject payload = new JSONObject();
|
||||||
payload.put("pageId", pageId);
|
payload.put("pageId", pageId);
|
||||||
payload.put("wrappedEvent", message);
|
payload.put("wrappedEvent", message);
|
||||||
|
@ -161,7 +162,7 @@ public class InspectorPackagerConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void sendEvent(String name, Object payload)
|
private void sendEvent(String name, Object payload)
|
||||||
throws JSONException, IOException {
|
throws JSONException {
|
||||||
JSONObject jsonMessage = new JSONObject();
|
JSONObject jsonMessage = new JSONObject();
|
||||||
jsonMessage.put("event", name);
|
jsonMessage.put("event", name);
|
||||||
jsonMessage.put("payload", payload);
|
jsonMessage.put("payload", payload);
|
||||||
|
@ -276,12 +277,21 @@ public class InspectorPackagerConnection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void send(JSONObject object) throws IOException {
|
public void send(final JSONObject object) {
|
||||||
if (mWebSocket == null) {
|
new AsyncTask<WebSocket, Void, Void>() {
|
||||||
return;
|
@Override
|
||||||
}
|
protected Void doInBackground(WebSocket... sockets) {
|
||||||
|
if (sockets == null || sockets.length == 0) {
|
||||||
mWebSocket.sendMessage(RequestBody.create(WebSocket.TEXT, object.toString()));
|
return null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
sockets[0].sendMessage(RequestBody.create(WebSocket.TEXT, object.toString()));
|
||||||
|
} catch (IOException e) {
|
||||||
|
FLog.w(TAG, "Couldn't send event to packager", e);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}.execute(mWebSocket);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void abort(String message, Throwable cause) {
|
private void abort(String message, Throwable cause) {
|
||||||
|
|
Loading…
Reference in New Issue