Я пытаюсь отправить объект JSON
с android
на сервер, который я написал в NodeJS
. сервер запущен и работает, я могу подключить к нему POST
с помощью программного обеспечения Postman
. но когда я пытаюсь отправить JSON
, используя android, я получаю SSLProtocolException: SSL handshake aborted
.
полный журнал:
javax.net.ssl.SSLProtocolException: SSL handshake aborted: ssl=0xd7a38728: Failure in SSL library, usually a protocol error
error:100000f7:SSL routines:OPENSSL_internal:WRONG_VERSION_NUMBER (third_party/openssl/boringssl/src/ssl/tls_record.cc:242 0xbc4ddce6:0x00000000)
Я использовал код отсюда пытается отправить http-запрос: Как отправить json объект на сервер из моего android приложения
после того, как я получил SSLProtocolException
Я попробовал эти решения:
Ошибка квитирования исключения SSLSocket при попытке отправки на SSLServerSocket
Javax. net .ssl .SSLHandshakeException: javax. net .ssl.SSLProtocolException: рукопожатие SSL прервано: сбой в библиотеке SSL, обычно ошибка протокола
javax. net. ssl.SSLException: подтверждение SSL прервано на android старых устройствах
, но, похоже, ничего не работает.
Я работаю на localhost, но он сказал один раз я не могу подключиться к локальному хосту, поэтому я изменил URL-адрес в приведенном ниже коде.
мой код:
public class SendProductJsonTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... strings) {
String data = "";
HttpURLConnection httpURLConnection = null;
try {
httpURLConnection = (HttpURLConnection) new URL(strings[0]).openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream());
wr.writeBytes("PostData="+strings[1]);
wr.flush();
wr.close();
InputStream in = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(in);
int inputStreamData = inputStreamReader.read();
while (inputStreamData != -1) {
char current = (char) inputStreamData;
inputStreamData = inputStreamReader.read();
data += current;
}
} catch (Exception e) {
Log.d("ccb", "doInBackground16: "+e.getCause());
Log.d("ccb", "doInBackground17: "+e.getMessage());
e.printStackTrace();
} finally {
if (httpURLConnection != null) {
httpURLConnection.disconnect();
}
}
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.d("ccb", "onPostExecute: "+s);
}
}
как я вызов http-запроса: * 1 051 *
findViewById(R.id.sendBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO: in a click it will gather all data from the user, turn it into a json object, and will send it to the DB;
nameOfProduct = ((EditText)findViewById(R.id.etNameOfPr)).getText().toString();
serialOfProduct = ((EditText)findViewById(R.id.etSerialOfPr)).getText().toString();
priceOfProduct = ((EditText)findViewById(R.id.etPriceOfPr)).getText().toString();
try {
productJson(nameOfProduct, serialOfProduct, priceOfProduct, getGenderRB(), uriList, getColorsCB(), getSizeCB(), new JSONMadeListener() {
@Override
public void onJSONMade(JSONObject object) {
try {
SSLContext.getInstance("TLSv1.2");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
try {
ProviderInstaller.installIfNeeded(getApplicationContext());
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
new SendProductJsonTask().execute("https://10.0.2.2:4000/products/add",object.toString());
}
});
} catch (JSONException e) {
e.printStackTrace();
}