我想做什么:
目前我已成功加载网址
Myclass.java
public class Myclass extends ActionBarActivity{
public static Context context = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act_buycredits);
context = this;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setBackgroundDrawable(
new ColorDrawable(getResources().getColor(
R.color.windhya_actionbar_color)));
// ////////////////-------SUPORT---MENU----IRRESPECTIVE--OF---VERSIONS---////////////////////////////
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
// ////////////////-------SUPORT---MENU----IRRESPECTIVE--OF---VERSIONS---////////////////////////////
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
switch (itemId) {
case android.R.id.home:
finish();
// Toast.makeText(this, "home pressed", Toast.LENGTH_LONG).show();
break;
}
return true;
}
@Override
public void onBackPressed() {
MyWebViewFragment fragment = (MyWebViewFragment)getFragmentManager().findFragmentById(R.id.myweb_fragment);
WebView webView = fragment.myWebView;
if(webView.canGoBack()){
webView.goBack();
}else{
super.onBackPressed();
}
}
static public class MyWebViewFragment extends Fragment {
WebView myWebView;
//final static String myBlogAddr = "https://www.payumoney.com/";
//final static String myBlogAddr = "https://entech1.cloudapp.net:8001/paymentGateWay/";
final static String myBlogAddr = Keys.pamentGateway_api;
String myUrl;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_webfragment, container, false);
myWebView = (WebView)view.findViewById(R.id.mywebview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new MyWebViewClient());
if(myUrl == null){
myUrl = myBlogAddr;
}
Map <String, String> extraHeaders = new HashMap<String, String>();
extraHeaders.put("phonenumber",AppController.getPhoneNumber());
extraHeaders.put("authtoken",AppController.getAuthCode());
this.myWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
this.myWebView.getSettings().setPluginState(android.webkit.WebSettings.PluginState.ON_DEMAND);
this.myWebView.getSettings().setJavaScriptEnabled(true);
this.myWebView.getSettings().setDomStorageEnabled(true);
this.myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
this.myWebView.setWebChromeClient(new WebChromeClient(){
@Override
public boolean onJsAlert(WebView view, String url,
String message, JsResult result) {
// TODO Auto-generated method stub
Toast.makeText(context, message, 5000).show();
//Toast.makeText(context, super.onJsAlert(view, url, message, result)+"", 5000).show();
return false;
//return super.onJsAlert(view, url, message, result);
}
});
myWebView.loadUrl(myUrl,extraHeaders);
return view;
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
}
}
如何实现
答案 0 :(得分:0)
从webview中捕获json响应
无法从WeView捕获html文本。
但你可以这样试试:
1。设置setWebViewClient
以从webview获取当前网址:
this.myWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if [url is last page which contain json string]
THEN
use HttpClient to get string from url
END
ELSE
view.loadUrl(url);
return true;
}
});
2. 在shouldOverrideUrlLoading
中检查url
是否是最后一页,然后使用{G}请求(HttpGet)使用HttpClient
获取响应字符串。
答案 1 :(得分:-1)
首先覆盖WebViewClient的shouldOverrideUrlLoading()
和onPageFinished
@override
public void onPageFinished (WebView view, String url) {
isContent= (url.contains("filename/urlpattern"));
}
@Override
public boolean shouldOverrideUrlLoading (WebView view, String url) {
if(isContent) {
URL aUrl = new URL(url);
URLConnection conn = aUrl.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
// Use inputstream code to read the json..
return true;
}
return false;
}