gpt4 book ai didi

android - 从 webview 捕获一个 json 响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:31:06 26 4
gpt4 key购买 nike

我正在尝试做的事情:

  • 我正在将 url 加载到 webview 中
  • 我正在 webview 中遍历 2 3 个页面
  • 在最后一个页面中完成了一个事务并且一个 json 响应是返回
  • 我需要捕获那个回应

如何在 ANDROID 中实现这一点?

目前我已经成功加载了url

我的类.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);
}

}
}

如何实现这一目标

最佳答案

Catching a json response from a webview

无法从 WeView 捕获 html 文本。

不过你可以这样试试:

1. 设置setWebViewClient从webview获取当前url:

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 是否为最后一页然后使用 HttpClient 使用 HTTP GET 请求 (HttpGet) > 获取响应字符串。

关于android - 从 webview 捕获一个 json 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29003408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com