这是场景。用户在我无法控制的网站上拥有一个帐户。我的应用需要来自http://url/json的JSON数据。仅当用户登录时才显示JSON。
基本上,我的应用将: 1.弹出登录页面(如果可能,请在Web视图中,甚至在浏览器中) 2.他们将输入凭据(我不需要或不需要这些) 3.我将使用经过身份验证的会话来获取JSON数据。 (重新格式化和显示JSON数据是用户使用此应用程序的全部原因,因此他们会满意的。)
如果没有特定的面向外部的API,这是否有可能? Webview /浏览器可以访问数据,因此我想也许我的应用程序也可以(在用户的明确许可下)。
我还没有开始,所以我对大多数Android开发都持开放态度。我倾向于在多平台方面使用React-Native,这对于我的工作学习会有所帮助。但是,在访问此类数据时可能会受到更多限制。任何建议表示赞赏。
答案 0 :(得分:1)
首先,您需要将url加载到webview并检查登录是否成功(onPageFinish也可以注入javascript以获取直接json),然后可以使用asyncTask获取数据
webView.getSettings().setJavaScriptEnabled(true); //
webView.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int
errorCode, String description, String
failingUrl) {
Toast.makeText(context, description, Toast
.LENGTH_SHORT).show();
}
@TargetApi(Build.VERSION_CODES.M)
@Override
public void onReceivedError(WebView view,
WebResourceRequest req,
WebResourceError error) {
onReceivedError(view, error.getErrorCode(), error
.getDescription().toString(), req.getUrl
().toString());
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
HttpGetResources httpGet = new HttpGetResources(new AsyncResponse()
{
@Override
public void processFinish(Object output, Exception e) {
if (output != null) {
//do something
} else {
Toast.makeText(context,
getString(R.string.server_not_responding),
Toast.LENGTH_LONG).show();
}
}
});
Log.e("URL : ", URL);
httpGet.execute(URL);
}
});
webView.loadUrl(URL);
-AsyncTask类-
public class HttpGetResources extends AsyncTask<String, Void, Object> {
private AsyncResponse delegate = null;
private static final String REQUEST_METHOD = "GET";
private static final int READ_TIMEOUT = 15000;
private static final int CONNECTION_TIMEOUT = 15000;
private static final String RAW_DATE_FORMAT = "yyyy-MM-
dd'T'HH:mm:ss.SSSz";
private static final Gson GSON = new
GsonBuilder().setDateFormat(RAW_DATE_FORMAT).create();
private Exception exception=null;
private BufferedReader buffer = null;
public HttpGetResources(AsyncResponse asyncResponse) {
delegate = asyncResponse;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Object response) {
super.onPostExecute(response);
delegate.processFinish(response,exception);
}
@Override
protected Object doInBackground(String... params) {
String stringUrl = params[0];
Object result;
try {
URL myUrl = new URL(stringUrl);
HttpURLConnection connection = (HttpURLConnection)
myUrl.openConnection();
connection.setRequestMethod(REQUEST_METHOD);
connection.setReadTimeout(READ_TIMEOUT);
connection.setConnectTimeout(CONNECTION_TIMEOUT);
connection.connect();
buffer = new BufferedReader(new
InputStreamReader(connection.getInputStream()));
result = GSON.fromJson(buffer, TopHeadlines.class);
buffer.close();
buffer = null;
} catch (Exception e) {
e.printStackTrace();
exception=e;
Log.e("ERROR : ",e.toString() + e.getClass());
result = null;
}
return result;
}
public Exception returnException(){
return exception;
}
}
---接口异步响应---
public interface AsyncResponse {
void processFinish(Object output, Exception e);
}