发现一个大问题,同一个代码,在java项目里能有东西输出,在安卓项目中却输出为空
public static String request(String httpUrl, String httpArg) {
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "您自己的apikey");
connection.setDoOutput(true);
connection.getOutputStream().write(httpArg.getBytes("UTF-8"));
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
就简单的post代码,一个参数是接口地址,一个参数是post内容,放在Android studio创的安卓项目里,完全没东西输出(返回的字符串是空),该加的网络权限全都有加,就是没东西返回。可是复制代码到JAVA项目,却有字符串返回。百思不得其解。求大神解答,想了一下午了。