简体   繁体   English

Android HTTP发布多部分

[英]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. 我想发出HTTP POST请求,以从客户端发送数据并将其放入表单。 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. 问题是,当我阅读响应时,得到的是我请求的网站的HTML代码,但没有成功代码或类似内容。 It looks like I am requesting for site content, but not submitting the form. 看起来我正在请求网站内容,但未提交表单。 Any idea what I am doing wrong? 知道我在做什么错吗?

You're submitting to a .html file. 您正在提交到.html文件。 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, eg a php script. 要处理表单提交,您必须提交到专门设计用于处理该提交的脚本或其他程序,例如php脚本。

OK, to clarify what Marc B said: action="dodajN.html" is almost certainly wrong. 好,澄清一下Marc B所说的话: action="dodajN.html"几乎可以肯定是错误的。 I've never seen a web server that lets you do this (of course, anything is possible). 我从未见过可让您执行此操作的Web服务器(当然,一切皆有可能)。 It should probably be action="cgi-bin/something" or something like that. 它可能应该是action="cgi-bin/something"或类似的东西。

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. 但是,实际上并不是那么重要,因为您的应用程序无论如何都不使用action子句,而是写入“ url”,这甚至是错误的。 If you would tell us exactly what url you're really writing to, it might help. 如果您确切地告诉我们您实际上要写入的URL,则可能会有所帮助。

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. 通常,当我开发类似这样的东西时,我首先编写服务器端cgi脚本和一个网页来使用它。 Once my API is working through the web page, only then do I start trying to call the cgi script from an Android app. 一旦我的API遍历网页后,我才开始尝试从Android应用程序调用cgi脚本。

My debugging process consists of: 1) Reading the server logs. 我的调试过程包括:1)读取服务器日志。 2) Having my cgi scripts write their own debug logs. 2)让我的cgi脚本编写自己的调试日志。 3) Having my android app dump the response code and headers to logcat. 3)让我的Android应用程序将响应代码和标头转储到logcat。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM