implementation 'org.apache.httpcomponents:httpclient:4.5.13' implementation 'org.apache.httpcomponents:httpmime:4.5.13' private class HttpAsyncTask extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... urls) { String result; HttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(urls[0]); try { HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); result = EntityUtils.toString(httpEntity); } catch (IOException e) { result = "Error: " + e.getMessage(); } return result; } @Override protected void onPostExecute(String result) { Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show(); } } public void sendRequest() { String url = "http://www.example.com/api/data"; HttpAsyncTask asyncTask = new HttpAsyncTask(); asyncTask.execute(url); } <uses-library android:name="org.apache.http.legacy" android:required="false" />


上一篇:
下一篇:
切换中文