android HTTP post multipart

2019-09-19 09:48发布

问题:

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?

回答1:

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.



回答2:

OK, to clarify what Marc B said: action="dodajN.html" is almost certainly wrong. I've never seen a web server that lets you do this (of course, anything is possible). It should probably be action="cgi-bin/something" or something like that.

It's actually not that important however, since your app isn't using the action clause anyway, but rather writing to "url" which is even more wrong. If you would tell us exactly what url you're really writing to, it might help.

But ultimately, the way you debug this is to look at the server logs and see what's happening at that end.

As a general rule, when I'm developing something like this, I first write the server-side cgi script and a web page to use it. Once my API is working through the web page, only then do I start trying to call the cgi script from an Android app.

My debugging process consists of: 1) Reading the server logs. 2) Having my cgi scripts write their own debug logs. 3) Having my android app dump the response code and headers to logcat.