Я дважды проверил свой URL и размер запроса, оба работают нормально с POSTMAN, но выдают ошибку в Android Studio.
Эта функция разбирает jsonObject на строку
private static String encodeParams(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
public static String sendPost(String r_url , JSONObject postDataParams) throws Exception {
URL url = new URL(r_url);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(20000);
conn.setConnectTimeout(20000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8"));
String res= encodeParams(postDataParams);
writer.write(res);
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode(); // To Check for 200
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader( new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
return null;
}
Здесьмой объект и URL:
final JSONObject object = new JSONObject();
object.put("Role", "Admin");
object.put("Email", "Swaiarashad@gmail.com");
object.put("Password", "dfsdfsc231PW");
String http = "http://" + constants.ip + "/WcfService/Service1.svc/signin";
return webService.sendPost(http,object);
URL: http://192.168.100.110/WcfService/Service1.svc/signin