Android web设置以编程方式输入和读取数据

时间:2015-01-10 13:57:58

标签: android html web input

我希望我的应用程序在网页上设置输入(选择框),以便可以重新生成结果-htmlcode。 Htmlcode看起来像这样:

<select name="element" class="selectbox" style="width:114" size="12" onchange="doDisplayTimetable(NavBar, topDir);">  
<option value="1">5a</option>  
<option value="2">5b</option>  
<option value="3">5c</option>  
<option value="4">5d</option>  
<option value="5">5e</option>  
<option value="6">6a</option>  
<option value="7">6b</option>   
<option value="8">6c</option>  
<option value="9">6d</option>  
<option value="10">7a</option>  
<option value="11">7b</option>  
<option value="12">7c</option>  
<option value="13">7d</option>  
<option value="14">7e</option>  
<option value="15">8a</option>  
<option value="16">8b</option>  
<option value="17">8c</option>  
<option value="18">8d</option>  
<option value="19">8e</option>  
<option value="20">9a</option>  
<option value="21">9b</option>  
<option value="22">9c</option>  
<option value="23">9d</option>

应用程序现在应该选择23然后将Htmlcode存储在字符串中。请帮助:)

1 个答案:

答案 0 :(得分:0)

String downloadURL = "your url";
try {
            URL url = new URL(downloadURL);
            String urlParameters = "";
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            connection.setRequestProperty("Content-Length",
                    "" + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            InputStream inputStream = connection.getInputStream();
            //This inputstream contains html
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            workingInternet = false;

        }

urlParameters字符串的格式为

String urlParameters = "inputBoxname1"
                    + URLEncoder.encode(inputValue1, "UTF-8")
                    + "&inputBoxname2="
                    + URLEncoder.encode(inputValue2, "UTF-8");