I need to send data to the mail (more precisely to the server, and there the server to the mail) POST request with parameters. Please help me how to do this, the error code does not issue, I can not understand what exactly is wrong. No response from the server is needed, just send the data.

Here is the code:

@Override protected Void doInBackground(String... params) { try { String paramss = "name=" + params[0] + "@number=" + params[1]; URL url = new URL("http://177.177.177.237:86/send/"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Length", paramss); connection.connect(); Log.e("TAG", "Opa " + connection.getResponseCode()); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } 

As a result, the code does not issue errors, nothing comes to the mail, the code 400 in the Opa logs

Another option was to enter the parameters in the URL, but also did not help

 URL url = new URL("http://177.127.177.237:86/send/name=" + params[0] + "@number=" + params[1]); 
  • And what do you do in connection.setRequestProperty("Content-Length", paramss); ? - s8am
  • And what you need to insert there, but lazhanulsya. "text / plain"? - T. Kudaibergen
  • I can be wrong, but this field does not have to be filled. Another thing is that you don’t pass on anything else. - s8am
  • I registered "text / plain" now to the post even though the message arrives, but the fields are empty, the parameters were not transferred - T. Kudaibergen
  • If you don’t fill out, how can you pass the parameters - T. Kudaibergen

1 answer 1

 URL url = new URL("http://177.127.177.237:86/send"); HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("POST"); conn.setDoInput(true); conn.setDoOutput(true); // формируем параметры : List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("firstParam", paramValue1)); params.add(new BasicNameValuePair("secondParam", paramValue2)); params.add(new BasicNameValuePair("thirdParam", paramValue3)); OutputStream os = conn.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(getQuery(params)); writer.flush(); writer.close(); os.close(); conn.connect();// только здесь передаем 

and convert the parameters to UTF-8:

 private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); boolean first = true; for (NameValuePair pair : params) { if (first) first = false; else result.append("&"); result.append(URLEncoder.encode(pair.getName(), "UTF-8")); result.append("="); result.append(URLEncoder.encode(pair.getValue(), "UTF-8")); } return result.toString(); } 

https://developer.android.com/reference/java/net/HttpURLConnection.html

  • NameValuePair is that I'm sorry, I have a red light - T. Kudaibergen
  • @ T.Kudaibergen, custom class - Vfvtnjd
  • Is NameValuePair not an obsolete way? - T. Kudaibergen