Search code examples
androidhtmlhttp-postmultipart

android HTTP post multipart


Hello, I have a website part described as below:

<div id="insertA">
    <form class="MultiFile-intercepted" enctype="multipart/form-data"
        method="post" onsubmit="return checkAnomalyFields();"
        action="dodajN.html">
        <table style="border-weight: 0px">
            <tbody>
                <tr>
                <tr>
                    <td id="wybory"><select id="typ" onchange="typeSelected()" size="1"
                        name="typuId">
                    </td>
                    <td>
                </tr>
                <tr>
                    <td>Szerokość: <input id="szer" type="text" onchange="setMarker()"
                        value="" name="szer">
                        <div id="szerErr" class="err">Proszę podać szerokość na terenie
                            Polski (49-55).</div>
                    </td>
                    <td>Długość: <input id="dlug" type="text" onchange="setMarker()"
                        value="" name="dlug">
                        <div id="dlugErr" class="err">Proszę podać długość na terenie
                            Polski (14-25).</div> <input id="id" type="hidden" value=""
                        name="id">
                    </td>
                </tr>

I want to make a HTTP POST request to send data from my client and put it into forms. I am doing this as follows:

try {
    HttpClient client = new MyHttpClient(Send.this);  
    String postURL = "url";
    HttpPost post = new HttpPost(postURL); 
    //FileBody bin = new FileBody(file);
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);  
    //reqEntity.addPart("myFile", bin);
    reqEntity.addPart("typuId", new StringBody("1"));
    reqEntity.addPart("statusuId", new StringBody("2"));
    reqEntity.addPart("szer", new StringBody("52.321911"));
    reqEntity.addPart("dlug", new StringBody("19.464111000000003"));
    reqEntity.addPart("opis",  new StringBody("jakis opis"));

    post.setEntity(reqEntity);  
    HttpResponse response = client.execute(post);  
    HttpEntity resEntity = response.getEntity();  
    AlertDialog.Builder alert=new AlertDialog.Builder(Send.this);
    alert.setTitle("Niepoprawne dane").setMessage(EntityUtils.toString(resEntity)).setNeutralButton("OK", null).show();

    if (resEntity != null) {    
        Log.i("RESPONSE",EntityUtils.toString(resEntity));
    }
} catch (Exception e) {
    e.printStackTrace();
}

The problem is when I read the response I get the HTML code of the site that I am requesting without a success code or anything similar. It looks like I am requesting for site content, but not submitting the form. Any idea what I am doing wrong?


Solution

  • You're submitting to a .html file. Generally servers aren't configured to treat those files as scripts, which means the data you're submitting is simply ignored and dumped. To handle a form submission, you have to submit to a script or other program specifically designed to handle that submission, e.g. a php script.