从 android 填充网站上的文本字段而不显示 WebView
Populating text fields on a website from android without showing a WebView
有没有办法在没有 loading/showing WebView 的情况下从 android 将值放入网页上的文本字段?例如,我有两个字符串,想在网页上填充两个字段,有没有办法做到这一点 "in the background" 或者换句话说,不必打开用户看到的 window?
谢谢
更新
在 Tango 的帮助下找到解决方案
这是我的 OkHttpHandler class:
public class OkHttpHandler extends AsyncTask<String, Void, String> {
OkHttpClient client = new OkHttpClient();
String cardNumber, privNumber;
public OkHttpHandler(String cardNumber, String privNumber) {
this.cardNumber = cardNumber;
this.privNumber = privNumber;
}
@Override
protected String doInBackground(String... strings) {
RequestBody formBody = new FormBody.Builder()
//Change .add first parameter to the text field you want to populate
.add("CardNbr", cardNumber)
.add("CardPin", privNumber).build();
Request request = new Request.Builder().url(strings[0])
.post(formBody).build();
try {
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code "+ request.toString());
return response.body().string();
} catch (Exception e) {
}
return null;
}
}
我的 MainActivity Class 的一个片段,我将响应打印到日志
OkHttpHandler handler = new OkHttpHandler(items.get(0).getNumber(), items.get(0).getPrivNumber());
String result = "";
try {
result = handler.execute("urlHere.com").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.d("returned value", result);
使用OkHttp
final OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("field to be filled", "content")
.build();
Request request = new Request.Builder()
.url("webpage url")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
有没有办法在没有 loading/showing WebView 的情况下从 android 将值放入网页上的文本字段?例如,我有两个字符串,想在网页上填充两个字段,有没有办法做到这一点 "in the background" 或者换句话说,不必打开用户看到的 window? 谢谢
更新
在 Tango 的帮助下找到解决方案
这是我的 OkHttpHandler class:
public class OkHttpHandler extends AsyncTask<String, Void, String> {
OkHttpClient client = new OkHttpClient();
String cardNumber, privNumber;
public OkHttpHandler(String cardNumber, String privNumber) {
this.cardNumber = cardNumber;
this.privNumber = privNumber;
}
@Override
protected String doInBackground(String... strings) {
RequestBody formBody = new FormBody.Builder()
//Change .add first parameter to the text field you want to populate
.add("CardNbr", cardNumber)
.add("CardPin", privNumber).build();
Request request = new Request.Builder().url(strings[0])
.post(formBody).build();
try {
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code "+ request.toString());
return response.body().string();
} catch (Exception e) {
}
return null;
}
}
我的 MainActivity Class 的一个片段,我将响应打印到日志
OkHttpHandler handler = new OkHttpHandler(items.get(0).getNumber(), items.get(0).getPrivNumber());
String result = "";
try {
result = handler.execute("urlHere.com").get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
Log.d("returned value", result);
使用OkHttp
final OkHttpClient client = new OkHttpClient();
RequestBody formBody = new FormBody.Builder()
.add("field to be filled", "content")
.build();
Request request = new Request.Builder()
.url("webpage url")
.post(formBody)
.build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);