安卓中进行网络请求的代码通常使用HttpURLConnection、OkHttp或Volley等库。以下是使用HttpURLConnection进行网络请求的示例代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetworkUtils {
public static String fetchData(String urlStr) {
StringBuffer response = new StringBuffer();
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
response.append(line);
}
in.close();
}
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return response.toString();
}
}
通过调用fetchData
方法可以进行网络请求,示例:
String response = NetworkUtils.fetchData("https://api.example.com/data");
以上是一个简单的使用HttpURLConnection进行网络请求的示例,你也可以根据具体需求选择合适的网络请求库。
以下是一个使用安卓进行网络请求的示例代码:
import android.os.AsyncTask;
import android.util.Log;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class NetworkRequestTask extends AsyncTask<String, Void, String> {
private static final String TAG = "NetworkRequestTask";
@Override
protected String doInBackground(String... urls) {
String result = null;
HttpURLConnection connection = null;
try {
URL url = new URL(urls[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
result = readInputStream(inputStream);
} else {
Log.e(TAG, "HTTP request failed with response code: " + responseCode);
}
} catch (IOException e) {
Log.e(TAG, "Error occurred during network request: " + e.getMessage());
} finally {
if (connection != null) {
connection.disconnect();
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
// 处理请求结果
if (result != null) {
Log.d(TAG, "Received response: " + result);
}
}
private String readInputStream(InputStream inputStream) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
bufferedReader.close();
inputStreamReader.close();
return stringBuilder.toString();
}
}
以上代码是一个名为NetworkRequestTask
的AsyncTask
子类,可以在后台线程中执行网络请求,并在请求完成后返回结果。你可以通过创建NetworkRequestTask
的实例,并调用execute()
方法来执行网络请求。

注意,上述代码仅用于演示目的,并未做异常处理和参数验证等。在实际开发中,你可能需要进行一些额外的处理,以确保网络请求的可靠性和安全性。
发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/139683.html