0

我是编程的初学者,我想知道是否有人可以帮助我完成我的代码。我希望执行以下操作:

当用户第一次启动应用程序时:

  1. 用户会收到一个提示,要求他们输入用户名。然后必须保存用户名,以便当他们重新启动应用程序时,他们不会再次收到提示。
  2. 我希望我在第一步中输入的内容执行到登录网站页面的帖子,以便网站读取输入的内容,如果看到用户名,它会登录该人。这将有助于创建 SSO。在 webview 指向的网站标题中,它正在从 webview 中寻找以下变量来执行发布:和 2 个工作。

有人很友善地向我指出了以下似乎提供解决方案的链接,但是,我很难将其实施到我的代码中。链接:

将本地 HTML 文件加载到 WebViewJavaScript 发布请求中,如表单提交

提前感谢所有审阅我的代码并帮助我完成代码的人。也觉得要清理它,因为我确定它是一团糟。代码:

MainActivity.JAVA

package com.example.removed;
import java.net.URLEncoder;
import org.apache.http.util.EncodingUtils;
import android.net.Uri; import android.os.Bundle; import android.content.DialogInterface; import android.content.Intent;
import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.text.Editable; import android.view.KeyEvent; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.EditText;
public class MainActivity extends Activity {
private WebView myWebView;

@SuppressLint("SetJavaScriptEnabled") @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

myWebView = (WebView) findViewById(R.id.webview);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new HelloWebViewClient());
if (savedInstanceState!=null){
myWebView.restoreState(savedInstanceState);
}else{ 
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("login");
alert.setMessage("Please enter your User Name");

// Set an EditText view to get user input 
final EditText inf_username = new EditText(this);
alert.setView(inf_username);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
  String value = inf_username.getText().toString();
  // Do something with value!
  String postData = "username=inf_username";
  myWebView.postUrl("removed",EncodingUtils.getBytes(postData, "BASE64"));
    myWebView.loadUrl("removed");
    myWebView.setWebViewClient(new HelloWebViewClient());
  }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
  }
});

alert.show();


}
}
@Override
public void onSaveInstanceState(Bundle outState) {
//Toast.makeText(getBaseContext(), browser.getUrl(), Toast.LENGTH_LONG).show();
myWebView.saveState(outState);
} 

@Override
protected void onRestoreInstanceState(Bundle state) {
myWebView.restoreState(state);
super.onRestoreInstanceState(state);
//Toast.makeText(getBaseContext(), browser.getUrl(), Toast.LENGTH_LONG).show();
}
@Override
public void onResume(){
super.onResume();   
}


@Override
public void onPause() {
super.onPause();
}
private class HelloWebViewClient extends WebViewClient {


@Override
public boolean shouldOverrideUrlLoading(WebView webview, String url)
//{
    //webview.loadUrl(url);
    //return true;
//}
// } //public boolean shouldOverrideUrlLoading(WebView view, String url) 
{ if     (url.startsWith("tel:")) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); }else if(url.startsWith("mailto:")) { Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); }else if (url.startsWith("comgooglemaps")) { { url = "geo:" +url; } Intent openUrlIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(openUrlIntent); }else if(url.startsWith("http:") || url.startsWith("https:")) { webview.loadUrl(url); } return true;
}

}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK)&& myWebView.canGoBack())
{
    myWebView.goBack();
    return true;
}
return super.onKeyDown(keyCode, event);
}
}
4

0 回答 0