-2

I want to "translate" this HTML request post to Android:

<form name="myForm" action="https://mysite.example" method="POST">
  <input type="hidden" name="Key1" value=1>
  <input type="hidden" name="Key2" value="2">
  <input type="hidden" name="Key3" value="3">
</form>

Please note that the first value is an integer and not a String.

So I tried to use code found on stackoverflow:

public String  performPostCall(String requestURL, HashMap<String, String> postDataParams) {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);

        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write(getPostDataString(postDataParams));

        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        else {
            response="";
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return response;
}

private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
    StringBuilder result = new StringBuilder();
    boolean first = true;
    for(Map.Entry<String, String> entry : params.entrySet()){
        if (first)
            first = false;
        else
            result.append("&");

        result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
        result.append("=");
        result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
    }

    return result.toString();
}

In my AsyncTask I'm doing:

HashMap<String, String> parameters = new HashMap<>();
String url = "https://mysite.example";
String result;

parameters.put("Key1", "1");
parameters.put("Key2", "2");
parameters.put("Key3", "3");
result = performPostCall(url, parameters);

But this doesn't work. What's wrong?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
helloimyourmind
  • 994
  • 4
  • 14
  • 30

2 Answers2

0

I think you are missing an important line which makes the actual connection before your output stream

conn.connect();

you should also need to add the permission in your AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/> 
Dickens A S
  • 3,824
  • 2
  • 22
  • 45
0

you are closing the "writer" before it processes:

writer.flush();
writer.close();
os.close();

int responseCode=conn.getResponseCode();

John Bedoya
  • 31
  • 1
  • 3
  • Could you please clarify your answer? I don't see anything wrong with this block of code, except that there's no need to flush before calling `BufferedWriter.close()`. – Simon MᶜKenzie Feb 22 '18 at 01:32