Home > Mobile >  Novice asked HttpClient appear under the null pointer exception how to solve
Novice asked HttpClient appear under the null pointer exception how to solve

Time:10-14

Public class MainActivity extends AppCompatActivity implements the View. An OnClickListener {
TextView responseview;
Protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
The setContentView (R.l ayout. Mylayout);
The Button for the=(Button) the findViewById (R.i d.s end_button);
Responseview=(TextView) the findViewById (R.i d.r espose_text);
For the setOnClickListener (this);
}

Public void onClick (View v) {
If (v.g etId ()==R.i d.s end_button) {
SendRequestWithOkhttp ();
}
}
Private void sendRequestWithOkhttp () {
New Thread (new Runnable () {
@ Override
Public void the run () {
Try {
OkHttpClient client=new OkHttpClient ();
Request the Request=new Request. The Builder (). The url (" http://www.baidu.com "). The build ();
The Response the Response=client. NewCall (request). The execute ();
String responsedata=(https://bbs.csdn.net/topics/response.body). The String ();
ShowResponse (responsedata);
} the catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
Private void showResponse (final String response) {
RunOnUiThread (new Runnable () {
@ Override
Public void the run () {
Responseview. SetText (response);
}
});
}
}

CodePudding user response:

Has nothing to do with the code a bit more,,,,

CodePudding user response:

https://blog.csdn.net/weixin_42486039/article/details/81085437
Have a look.
  • Related