我有一个WebView,我正在尝试将后期响应作为字符串返回。我已经在Swift / iOS中成功实现了这个,但是我没能在Jave / Android中做到这一点。
在Swift / iOS中,我使用了shouldStartLoadWithRequest。我可以在哪里获得请求.HTTPBody。
Grabbing POST data from UIWebView
这是我的代码:但是当我实现这个时,我无法在我的WebView中导航。
aWebView = (WebView) findViewById(R.id.webView);
// Enable Javascript
WebSettings webSettings = aWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// Stop local links and redirects from opening in browser instead of WebView
aWebView.setWebViewClient(new WebViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//SHOW LOADING IF IT ISNT ALREADY VISIBLE
Log.d(LOGTAG, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(flag)
{
GetUserLoginTask GetUserLogin = new GetUserLoginTask(url);
GetUserLogin.execute();
return true;
}
return false;
}
@Override
public void onPageFinished (WebView view, String url) {
flag = true;
}
});
aWebView.loadUrl(mainUrl);
然后我使用and asynctask进行网络通信:
public class GetUserLoginTask extends AsyncTask<Void, Void, String> {
private final String mURL;
GetUserLoginTask(String url) {
mURL = url;
}
@Override
protected String doInBackground(Void... params) {
try {
URL aURL = new URL(mURL);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String s = response.toString();
Log.d(LOGTAG, s);
return s;
} catch (MalformedURLException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
@Override
protected void onPostExecute(String g) {
Log.d("elaegen", g);
}
}