我刚开始学习OkHttp。我的示例代码如下
public class MainActivity extends AppCompatActivity {
private static final String LOG_TAG = "OkHttp";
private TextView mTextView;
private Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = (TextView) findViewById(R.id.textView);
mHandler = new Handler(Looper.getMainLooper());
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new FormEncodingBuilder()
.add("grant_type", "password")
.add("username", "bnk")
.add("password", "bnk")
.build();
Request request = new Request.Builder()
.url("http://...")
.post(requestBody)
.build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
mTextView.setText(e.toString());
Log.e(LOG_TAG, e.toString());
}
@Override
public void onResponse(Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
final String message = jsonObject.toString(5);
Log.i(LOG_TAG, message);
mHandler.post(new Runnable() {
@Override
public void run() {
mTextView.setText(message);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
我知道我应该使用Handler
setText
来TextView
onResponse
onFailure
,但是Handler
我不会使用onFailure
{1}}并在调用android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
时检查结果。奇怪的是,有时我的应用程序与TextView
崩溃,有时可行(我的意思是Exception
可以显示Looper.myLooper() == Looper.getMainLooper()
)。
我是OkHttp的新手。
从下面的@NightlyNexus评论中,我尝试检查FALSE
,结果始终是//HTML file
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script src=angularfiles/angular.min.js"></script>
<script src="bootstrap/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/0.5.1/js/ngDialog.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ng-dialog/0.5.1/css/ngDialog.css"></script>
<script src="jsfiles/console.js"></script>
</head>
<body ng-app="myapp" ng-controller="controller" >
<div class="container-fluid col-lg-2" style="background-color:#848482;padding-top:25px;height:657px">
<!-- <button class="btn btn-primary" style="background-color: black;" ng-click="">Controlls</button> -->
<h4>Controlls:</h4>
<button class="btn btn-primary" ng-dialog="firstDialogId"
ng-dialog-controller="InsideCtrl"
ng-dialog-data="{{jsonData}}"
ng-dialog-class="ngdialog-theme-default"
ng-dialog-show-close="false">Open via directive</button>
</div>
</body>
</html>
//js file
var app=angular.module("myapp",['ngDialog']);
myapp.controller("controller",["$scope",function($scope){
$scope.clickme=function(){
console.log("hi");
ngDialog.open({
template: 'externalTemplate.html',
controller: 'SomeController'
});
};
}]);
,但是,正如我所说,该应用仍然有效。请参阅以下屏幕截图: