Created
January 12, 2016 10:51
-
-
Save jonasevcik/bcf66675e9581620f7fa to your computer and use it in GitHub Desktop.
Demo of difference between accessing views from OkHttp 2 Callback and MainThreadCallback.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CallError extends RuntimeException { | |
public static CallError networkError(Request response, IOException exception) { | |
return new CallError(response, Kind.NETWORK, exception); | |
} | |
public static CallError httpError(Request response) { | |
return new CallError(response, Kind.HTTP, null); | |
} | |
public enum Kind { | |
/** | |
* An {@link java.io.IOException} occurred while communicating to the server. | |
*/ | |
NETWORK, | |
/** | |
* A non-200 HTTP status code was received from the server. | |
*/ | |
HTTP | |
} | |
private final Request request; | |
private final Kind kind; | |
CallError(Request request, Kind kind, Throwable throwable) { | |
super(throwable); | |
this.request = request; | |
this.kind = kind; | |
} | |
public Request getRequest() { | |
return request; | |
} | |
public Kind getKind() { | |
return kind; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class MainThreadCallback implements Callback { | |
private final Handler mHandler = new Handler(Looper.getMainLooper()); | |
@Override | |
public final void onFailure(final Request request, final IOException e) { | |
callFailure(CallError.networkError(request, e)); | |
} | |
@Override | |
public final void onResponse(final Response response) throws IOException { | |
if (response.isSuccessful() && response.body() != null) { | |
try { | |
final String body = response.body().string(); | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
success(body); | |
} | |
}); | |
} catch (final IOException e) { | |
callFailure(CallError.networkError(response.request(), e)); | |
} | |
} else { | |
callFailure(CallError.httpError(response.request())); | |
} | |
} | |
private void callFailure(final CallError error) { | |
mHandler.post(new Runnable() { | |
@Override | |
public void run() { | |
failure(error); | |
} | |
}); | |
} | |
public abstract void success(String responseBody); | |
public abstract void failure(CallError error); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class OkHttpActivity extends AppCompatActivity { | |
private OkHttpClient mClient = new OkHttpClient(); | |
private TextView mResponseText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_ok_http); | |
mResponseText = (TextView) findViewById(R.id.response_text); | |
} | |
//onClick listener for a Button, crashes with "android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views." | |
public void normalCallback(View view) { | |
mClient.newCall(new Request.Builder().url("http://drive.google.com/uc?export=view&id=0B2ZerSqwiAA-dGdWMk1RaFRlQTQ").build()).enqueue(new Callback() { | |
@Override | |
public void onFailure(Request request, IOException e) { | |
mResponseText.setText("failure"); | |
} | |
@Override | |
public void onResponse(Response response) throws IOException { | |
mResponseText.setText("success"); | |
} | |
}); | |
} | |
//onClick listener for a Button | |
public void mainThreadCallback(View view) { | |
mClient.newCall(new Request.Builder().url("http://drive.google.com/uc?export=view&id=0B2ZerSqwiAA-dGdWMk1RaFRlQTQ").build()).enqueue(new MainThreadCallback() { | |
@Override | |
public void success(String responseBody) { | |
mResponseText.setText("success"); | |
} | |
@Override | |
public void failure(CallError error) { | |
mResponseText.setText("failure"); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment