android - HttpClient 未从远程站点填写输入框

标签 android http-post httpclient

我创建了一个应用程序,用户在其中输入名称,按下发送按钮时,远程服务器上的表单将根据用户输入进行填写。但不知何故它不起作用,它没有填充输入框,我没有收到任何错误,所以我不确定出了什么问题。

这是我的 mainactivity.java 代码

public class MainActivity extends Activity {
    Button send;
    EditText name;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = (EditText) findViewById(R.id.etFName);
        send = (Button) findViewById(R.id.bSend);

        final String sname = name.getText().toString();

        send.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                new Thread(new Runnable()
                {
                    public void run()
                    {
                        // TODO Auto-generated method stub
                        HttpClient postClient = new DefaultHttpClient();
                        String postReq = "http://10.0.2.2/formcode.php";
                        HttpPost request = new HttpPost(postReq);
                        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
                        postParams.add(new BasicNameValuePair("element_8_1", sname));
                        UrlEncodedFormEntity postEntity = null;
                        try {
                            postEntity = new UrlEncodedFormEntity(postParams);
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }

                        request.setEntity(postEntity);

                        HttpResponse response = null;
                        try {
                            response = postClient.execute(request);
                        } catch (ClientProtocolException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

                String url = "http://10.0.2.2/formcode.php";
                Intent intent = new Intent(android.content.Intent.ACTION_VIEW,  Uri.parse(url));
                startActivity(intent);
            }
        });
    }
}

提前致谢。

已编辑

这是网站代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>App Test Form</title>
<link rel="stylesheet" type="text/css" href="./data/form_6/css/view.css" media="all" />
<script type="text/javascript" src="js/view.js"></script>
<script type="text/javascript" src="js/calendar.js"></script>
</head>
<body id="main_body" >

    <img id="top" src="images/top.png" alt="" />
    <div id="form_container">

        <h1><a>App Test Form</a></h1>
        <form id="form_6" class="digitalfuture"  method="post" action="#main_body">
                    <div class="form_description">
            <h2>App Test Form</h2>
            <p></p>
        </div>                      
            <ul >

                    <li id="li_8" >
        <label class="description">Name <span id="required_8" class="required">*</span></label>
        <span>
            <input id="element_8_1" name= "element_8_1" class="element text" maxlength="255" size="8" value="" />
            <label>First</label>
        </span>
        <span>
            <input id="element_8_2" name= "element_8_2" class="element text" maxlength="255" size="14" value="" />
            <label>Last</label>
        </span><p class="guidelines" id="guide_8"><small>Please tell us your name</small></p> 
        </li>       <li id="li_9" >
        <label class="description">Phone Number <span id="required_9" class="required">*</span></label>
        <span>
            <input id="element_9_1" name="element_9_1" class="element text" size="3" maxlength="3" value="" type="text" /> -
            <label for="element_9_1">(###)</label>
        </span>
        <span>
            <input id="element_9_2" name="element_9_2" class="element text" size="3" maxlength="3" value="" type="text" /> -
            <label for="element_9_2">###</label>
        </span>
        <span>
            <input id="element_9_3" name="element_9_3" class="element text" size="4" maxlength="4" value="" type="text" />
            <label for="element_9_3">####</label>
        </span>
        <p class="guidelines" id="guide_9"><small>Please tell us your phone number</small></p> 
        </li>       <li id="li_10" >
        <label class="description">Alternate Phone Number </label>
        <span>
            <input id="element_10_1" name="element_10_1" class="element text" size="3" maxlength="3" value="" type="text" /> -
            <label for="element_10_1">(###)</label>
        </span>
        <span>
            <input id="element_10_2" name="element_10_2" class="element text" size="3" maxlength="3" value="" type="text" /> -
            <label for="element_10_2">###</label>
        </span>
        <span>
            <input id="element_10_3" name="element_10_3" class="element text" size="4" maxlength="4" value="" type="text" />
            <label for="element_10_3">####</label>
        </span>
        <p class="guidelines" id="guide_10"><small>Please tell us an alternate phone number to contact you at</small></p> 
        </li>       <li id="li_11" >
        <label class="description" for="element_11">Email <span id="required_11" class="required">*</span></label>
        <div>
            <input id="element_11" name="element_11" class="element text medium" type="text" maxlength="255" value="<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cab3a5bfb88aafa7aba3a6e4a9a5a7" rel="noreferrer noopener nofollow">[email protected]</a>" /> 
        </div> 
        </li>

    </div>
    <img id="bottom" src="images/bottom.png" alt="" />
    </body>
</html>

最佳答案

我认为这可能是因为您在参数中发送的值为空。 sname 设置在 onClickListener 之外,因此单击按钮时该值不会更新。它只是获取文本框中已有的值。我建议将代码移至 AsyncTask (有很多关于这方面的好教程)正如 AnujAroshA 建议的那样。你想要这样的东西:

private class SubmitForm extends AsyncTask<String, Void, Void>{
    @Override
    protected Void doInBackground(String... params) {
                String sName = params[0];

                // Rest of the code you had here
            }
}

这将创建一个在后台运行而不是在 UI 线程上运行的任务。然后要运行它,只需修改 onClickListener 即可

public void onClick(View v) {
    new SubmitForm().execute(name.getText().toString());
}

那么这将真正开始请求。它与使用 Thread 的方式类似,但更加 Android 官方。

关于android - HttpClient 未从远程站点填写输入框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12701258/

相关文章:

java - 无法向对象添加/删除位图

java - 在 HttpURLConnection 中发送 PUT、DELETE HTTP 请求

javax.net.ssl.SSLPeerUnverifiedException : peer not authenticated in jdk7 but not in jdk8

.net - 将 HttpClient 与 SSL 结合使用 - 覆盖 Credential Required 弹出窗口

javascript - 使用 Android 中的 WebView 自动填写表单

sql - Android Sqlite语句问题

android - 将 RxJava Observable 绑定(bind)到 TextView 的文本属性

javascript - 如何使用 Fetch 发布 x-www-form-urlencoded 请求?

php - HTTP Post 将多个文件从 iOS 上传到 PHP

android - 除了 WebView.getSettings().getUserAgentString() 之外,有没有办法获取默认的用户代理字符串?