1. Action http request
2. Usage:
(1). Since it is developed by a third-party company, it is necessary to add the dependency of the OkHttp library to build.gradle
dependencies {
implementation 'com.squareup.okhttp3:okhttp:3.4.1'}
In older versions, implementation was compile
(2).
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sendRequest = (Button)findViewById(R.id.send_request); responseText = (TextView)findViewById(R.id.response_text); sendRequest.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { if(v.getId() == R.id.send_request){sendRequestWithOKHttp(); } } }); }
private void sendRequestWithOKHttp(){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url("http://192.168.135.2:10000/student.xml") .build(); Response response = client.newCall(request).execute(); String responseData = response.body().string(); showResponse(responseData); }catch (IOException e){ e.printStackTrace () ; } } }).start(); }