讯飞翻译API在Eclipse可用但Android Studio返回null排查求助
讯飞翻译API在Android Studio调用返回null但Eclipse正常的问题排查
我在使用讯飞翻译API时遇到问题:代码在Eclipse中运行正常,但在Android Studio中调用该API始终返回null。已确认请求的URL、请求头(header)、请求体(body)均不为空且内容正确,也检查了依赖、更换过测试设备,问题仍未解决。
相关代码
HttpUtil类
public static Map<String, Object> doPost2(String url, Map<String, String> header, String body) { Map<String, Object> resultMap = new HashMap<String, Object>(); PrintWriter out = null; try { URL realUrl = new URL(url); URLConnection connection = realUrl.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) connection; for (String key : header.keySet()) { httpURLConnection.setRequestProperty(key, header.get(key)); } httpURLConnection.setDoOutput(true); httpURLConnection.setDoInput(true); out = new PrintWriter(httpURLConnection.getOutputStream()); out.print(body); out.flush(); if (HttpURLConnection.HTTP_OK != httpURLConnection.getResponseCode()) { BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream())); System.out.println("failed,code:" + httpURLConnection.getResponseCode() + ",message:" + br.readLine()); return null; } String responseContentType = httpURLConnection.getHeaderField("Content-Type"); if ("audio/mpeg".equals(responseContentType)) { byte[] bytes = toByteArray(httpURLConnection.getInputStream()); resultMap.put("Content-Type", "audio/mpeg"); resultMap.put("sid", httpURLConnection.getHeaderField("sid")); resultMap.put("body", bytes); return resultMap; } else { BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream())); String line; String result = ""; while ((line = in.readLine()) != null) { result += line; } resultMap.put("Content-Type", "text/plain"); resultMap.put("body", result); return resultMap; } } catch (Exception e) { return null; } }
调用代码(Main)
Map<String, Object> resultMap = HttpUtil.doPost2(WebITS_URL, header, body);
排查方向与修复方法
Android网络权限与明文请求限制
- 确认
AndroidManifest.xml中已添加网络权限:<uses-permission android:name="android.permission.INTERNET"/> - 若API地址为HTTP(非HTTPS),Android 9+默认禁止明文请求,需在
application标签添加android:usesCleartextTraffic="true",或通过网络安全配置文件允许指定域名的明文请求。
- 确认
主线程网络请求限制
Android禁止在主线程执行网络操作,否则会抛出NetworkOnMainThreadException,但当前代码的catch块直接返回null,未暴露该异常。需将网络请求移至子线程执行,比如使用Thread、AsyncTask或Kotlin协程。补全异常日志
当前catch块仅返回null,无法定位具体错误原因。修改catch块,打印异常详情:catch (Exception e) { e.printStackTrace(); // 或使用Android Log工具:Log.e("HttpUtil", "请求失败", e); return null; }字符编码规范
读取响应流时未指定编码,可能导致乱码或读取失败。修改InputStreamReader指定UTF-8编码:// 错误流读取 BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getErrorStream(), "UTF-8")); // 正常响应流读取 BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));完善HttpURLConnection配置
添加超时配置,避免因连接超时无响应:httpURLConnection.setConnectTimeout(5000); // 5秒连接超时 httpURLConnection.setReadTimeout(10000); // 10秒读取超时优化请求体写入方式
使用PrintWriter写入请求体可能存在编码问题,改用OutputStream直接写入:OutputStream os = httpURLConnection.getOutputStream(); os.write(body.getBytes("UTF-8")); os.flush(); os.close();
内容的提问来源于stack exchange,提问作者QuinRa
相关产品推荐
相关产品推荐

