Common problems and solutions for connecting Java to Baidu AI interface
Abstract: With the rapid development of artificial intelligence technology, Baidu AI interface has become the first choice for many Java developers one of the tools. However, in the process of connecting to Baidu AI interface, we often encounter some problems. This article will introduce some common problems, give corresponding solutions, and provide some Java code examples for reference.
import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class AIAPIDemo { private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; private static final String API_URL = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify"; public static void main(String[] args) { try { URL url = new URL(API_URL + "?access_token=" + ACCESS_TOKEN); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/json"); String requestBody = "{"text":"这是一段测试文本"}"; OutputStream outputStream = connection.getOutputStream(); outputStream.write(requestBody.getBytes()); outputStream.close(); int responseCode = connection.getResponseCode(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(responseCode == 200 ? connection.getInputStream() : connection.getErrorStream())); String line; StringBuilder response = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { response.append(line); } bufferedReader.close(); System.out.println("Response: " + response.toString()); } catch (Exception e) { e.printStackTrace(); } } }
In the above code, we first specify the URL of the Baidu AI interface, and at the same time attach the access_token as the authentication information in the URL. Then, establish an HttpURLConnection connection and set the relevant request headers and request bodies. Finally, get the response content and output it.
import java.net.HttpURLConnection; import java.net.URL; public class AIAPITimeoutDemo { private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"; private static final String API_URL = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify"; public static void main(String[] args) { try { URL url = new URL(API_URL + "?access_token=" + ACCESS_TOKEN); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(10000); // 其他代码... } catch (Exception e) { e.printStackTrace(); } } }
In the above code, we use the setConnectTimeout
method to set the connection timeout to 5 seconds, and the setReadTimeout
method to set the connection timeout to 5 seconds. The read timeout is 10 seconds.
import com.google.gson.Gson; public class AIAPIJsonDemo { public static void main(String[] args) { String response = "{"result":{"positive_prob":0.898,"confidence":0.9,"negative_prob":0.102,"sentiment":0}}"; Gson gson = new Gson(); AIResult aiResult = gson.fromJson(response, AIResult.class); System.out.println("Sentiment: " + aiResult.result.sentiment); } } class AIResult { Result result; } class Result { double positive_prob; double confidence; double negative_prob; int sentiment; }
In the above code, we first define a class AIResult
to represent the result in the API return result
Field. Then, use Gson's fromJson
method to parse the JSON into a AIResult
object and get the value of the sentiment
field.
Summary: This article introduces common problems and solutions for Java to interface with Baidu AI interface, and gives some Java code examples. I hope it will be helpful to Java developers who are using Baidu AI interface.
The above is the detailed content of Common problems and solutions for connecting Java to Baidu AI interface. For more information, please follow other related articles on the PHP Chinese website!