0
我想发布一个JSON看起来像无法从POST JSON在HttpURLConnection的Android中
{
"latLong":"50.1109,8.6821 - latLong",
"currencyCode":"EUR",
"locale":"en-GB",
"budget":""
}
当我做这与邮差,我得到了我所需要的响应的响应。但在android studio中它不能编译,而我得到一个返回的空字符串。 我的功能如下所示。
protected Void doInBackground(Void... params) {
URL url = null;
StringBuffer sb = new StringBuffer();
try {
url = new URL("http://192.168.2.101:12345/services/test");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection con = null;
try {
con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setRequestProperty("Content-Type","application/json");
DataOutputStream printout = new DataOutputStream(con.getOutputStream());
String Json_String = "{\n" +
" \"latLong\":\"50.1109,8.6821 - latLong\",\n" +
" \"currencyCode\":\"EUR\",\n" +
" \"locale\":\"en-GB\",\n" +
" \"budget\":\"\"\n" +
"}";
InputStream in = new BufferedInputStream(con.getInputStream());
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
result = sb.toString();
printout.flush();
printout.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
responseMsg = con.getResponseMessage();
} catch (IOException e) {
e.printStackTrace();
}
try {
response = con.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}