使用java中的StringBuilder写一个demo
时间: 2024-05-16 22:13:16 浏览: 18
好的,请看下面的代码示例:
```java
public class StringBuilderDemo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Hello, ");
sb.append("world!");
System.out.println(sb.toString()); // 输出:Hello, world!
}
}
```
这个程序使用了 StringBuilder 类来构造一个字符串,然后向其追加了一个新的字符串,并将其打印输出。 StringBuilder 是一个可变的字符序列,可以方便地进行字符串拼接等操作。其他常用的字符串类还包括 String 和 StringBuffer 。
相关问题
帮忙使用java写个http发送请求的demo
好的,以下是使用Java发送HTTP请求的示例代码:
```java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpDemo {
public static void main(String[] args) {
try {
// 创建URL对象
URL url = new URL("http://example.com/api");
// 创建HTTP连接对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求方法为GET
connection.setRequestMethod("GET");
// 设置请求头参数
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer xxxxxxxx");
// 发送请求
int responseCode = connection.getResponseCode();
// 读取响应内容
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
// 输出响应内容
System.out.println(responseCode);
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
请将URL和请求头参数替换为您要访问的API的实际值。
先写一个java将json格式转formdata格式的demo
好的,以下是一个Java将JSON格式转换为FormData格式的示例代码:
```java
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
public class JsonToFormDataDemo {
public static void main(String[] args) {
JSONObject json = new JSONObject();
json.put("name", "John");
json.put("age", 30);
json.put("email", "john@example.com");
String formData = convertJsonToFormData(json);
System.out.println(formData);
}
private static String convertJsonToFormData(JSONObject json) {
StringBuilder formDataBuilder = new StringBuilder();
for (String key : json.keySet()) {
String value = json.get(key).toString();
try {
formDataBuilder.append(URLEncoder.encode(key, "UTF-8"))
.append("=")
.append(URLEncoder.encode(value, "UTF-8"))
.append("&");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
// 删除最后一个 "&" 符号
formDataBuilder.deleteCharAt(formDataBuilder.length() - 1);
return formDataBuilder.toString();
}
}
```
在上面的代码中,我们首先创建一个JSON对象,然后调用`convertJsonToFormData`方法将该JSON对象转换为FormData格式。在`convertJsonToFormData`方法中,我们使用`URLEncoder.encode`方法对每个键和值进行URL编码,并将它们以`key=value&`的格式添加到一个`StringBuilder`对象中。最后,我们从字符串生成器中删除最后一个"&"字符,并将其作为方法的返回值。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)