android能不能实现后台填写网页上的调查问卷?

android能不能实现后台填写网页上的调查问卷?

我最近工作上遇到一个难题,每季度要填写一个网站上100张左右的调查问卷,工作量不小(调查问卷要先登录账号密码,还有4个数字验证码)。我想能不能用android程序里面通过操作,快速的完成,网页开发我会jsp,不会javascript,还会一些java和android,我能通过现有的技术实现吗?

使用原生Webview 嵌套你会写的H5部分 用js桥进行交互

可以使用Android自带的WebView控件来加载网页,然后通过Android的自动化框架,来模拟用户在网页上的操作。

以下内容部分参考ChatGPT模型:


可以通过使用HttpClient或OkHttp库来模拟浏览器的行为,完成网页上的自动填写和提交操作。具体思路如下:

  1. 创建HttpClient或OkHttpClient对象,设置Cookie、User-Agent等相关参数,以模拟浏览器。

  2. 使用HttpGet或HttpPost请求获取登录页面,解析出页面中的登录表单,获取表单的提交地址、表单元素的名称和值等信息。

  3. 构造登录表单的参数,使用HttpPost请求提交登录表单,获取登录后的Cookie、Session等信息,保存到HttpClient或OkHttpClient对象中。

  4. 使用HttpGet或HttpPost请求访问调查问卷页面,解析出页面中的调查问卷表单,获取表单的提交地址、表单元素的名称和值等信息。

  5. 构造调查问卷表单的参数,使用HttpPost请求提交调查问卷表单,完成填写和提交操作。

下面是使用HttpClient模拟登录和提交的示例代码:

// 创建HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 设置Cookie、User-Agent等参数
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
httpClient.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0");

// 请求登录页面
HttpGet httpGet = new HttpGet("https://example.com/login");
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity);

// 解析登录表单
Document doc = Jsoup.parse(html);
String action = doc.select("form#login-form").attr("action");
String username = doc.select("input[name=username]").val();
String password = doc.select("input[name=password]").val();
String captcha = doc.select("input[name=captcha]").val();

// 构造登录表单参数
List<NameValuePair> formParams = new ArrayList<>();
formParams.add(new BasicNameValuePair("username", "your_username"));
formParams.add(new BasicNameValuePair("password", "your_password"));
formParams.add(new BasicNameValuePair("captcha", "your_captcha"));

// 提交登录表单
HttpPost httpPost = new HttpPost("https://example.com" + action);
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
response = httpClient.execute(httpPost);
entity = response.getEntity();
String cookie = response.getFirstHeader("Set-Cookie").getValue();

// 请求调查问卷页面
httpGet = new HttpGet("https://example.com/survey");
httpGet.setHeader("Cookie", cookie);
response = httpClient.execute(httpGet);
entity = response.getEntity();
html = EntityUtils.toString(entity);

// 解析调查问卷表单
doc = Jsoup.parse(html);
action = doc.select("form#survey-form").attr("action");
String answer1 = doc.select("input[name=answer1]").val();
String answer2 = doc.select("input[name=answer2]").val();

// 构造调查问卷表单参数
formParams.clear();
formParams.add(new BasicNameValuePair("answer1", "your_answer1"));
formParams.add(new BasicNameValuePair("answer2", "your_answer2"));

// 提交调查问卷表单
httpPost = new HttpPost("https://example.com" + action);
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
response = httpClient.execute(httpPost);
entity = response.getEntity();
html = EntityUtils.toString(entity);

使用OkHttp的示例代码类似,只是需要使用OkHttpClient对象和Request、Response等类来完成请求和响应。


如果我的建议对您有帮助、请点击采纳、祝您生活愉快

还需要安装App吗?很重的, 还不如搞个小程序或者web页面呢