原因是我填写了我的用户名,密码和点击登录按钮,它在页面上再次加载需要填写用户名,密码在这里是我的源代码
public class MainActivity extends Activity {
private EditText mTextUserName;
private EditText mTextPassword;
public String user_name;
public String pass_word;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextUserName = (EditText) findViewById(R.id.textUserName);
mTextPassword = (EditText) findViewById(R.id.textPassword);
final Button mButtonLogin = (Button) findViewById(R.id.buttonLogin);
mButtonLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
user_name = mTextUserName.getText().toString();
pass_word = mTextPassword.getText().toString();
// i am passing this intent
Intent goToNextActivity = new Intent(getApplicationContext(), ViewActivity.class);
goToNextActivity.putExtra("username", user_name);
goToNextActivity.putExtra("password", pass_word);
startActivity(goToNextActivity);
}
});
}
在我的ViewActivity中也是
public class ViewActivity extends Activity {
private WebView webView;
public String pass_word;
public String user_name;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setDefaultFontSize(20);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
//here i get them
getIntent().getStringExtra("username");
getIntent().getStringExtra("password");
webView.loadUrl("http://intranet.mdis.uz/");
}
但是这个有效((我花了8个小时进行研究,但没有任何帮助我
)答案 0 :(得分:1)
我认为这一行是错误的:
Intent goToNextActivity = new Intent(getApplicationContext(), ViewActivity.class);
尝试开始新活动时,不应使用ApplicationContext
。试试这个:
Intent goToNextActivity = new Intent(MainActivity.this, ViewActivity.class);
修改强>
试试这个,它应该通过POST发送数据:
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollbarFadingEnabled(false);
webView.getSettings().setDefaultFontSize(20);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
String username = getIntent().getStringExtra("username");
String password = getIntent().getStringExtra("password");
String data = "username=" + username + "&password=" + password;
webView.postUrl("http://intranet.mdis.uz/", EncodingUtils.getBytes(data, "base64"));
答案 1 :(得分:0)
让我试一试。
这样做一次修改,看看PLZ ......
public class MainActivity extends Activity {
private EditText mTextUserName;
private EditText mTextPassword;
public String user_name;
public String pass_word;
ConnectionClass cc;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextUserName = (EditText) findViewById(R.id.textUserName);
mTextPassword = (EditText) findViewById(R.id.textPassword);
cc=new ConnectionClass(this);
final Button mButtonLogin = (Button) findViewById(R.id.buttonLogin);
mButtonLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
user_name = mTextUserName.getText().toString();
pass_word = mTextPassword.getText().toString();
user_name=Uri.encode(user_name); // you can encode your parameters so that it does not throw exception for e.g it will replace a space as %20 otherwise if user enters special character error will come.. :)
pass_word=Uri.encode(pass_word); // same as above
String url="http://myserver.com/login.php?username="+user_name"&password="+user_name; // this is your server page url and passing username and password in plain url u can test it in browser to verify its working :)
new AsyncTask<String,Void,String>() // new android versions does not allow network operations on main thread but need to do asynchronously...
{
@Override
protected String doInBackground(String... params) {
return cc.getServerResponse(params[0]);
}
@Override
protected void onPostExecute(String result) { // this method runs on UI thread and is called when background process is done to make it more fancy u can use onPreExecute() to show a loader and dismiss the dialog here :)
super.onPostExecute(result);
if(result!=null)
{
if(result.equals("1") // assuming u r returning "1" if successful.
{
Toast.makeText(context, "Successful", 2000).show();
Intent goToNextActivity = new Intent(MainActivity.this, ViewActivity.class);
goToNextActivity.putExtra("username", user_name);
goToNextActivity.putExtra("password", pass_word);
startActivity(goToNextActivity);
}
}
else
Toast.makeText(context, "Network problem...", 2000).show();
}
}.execute(url);
}
}
});
}
这是ConnectionClass
public class ConnectionClass {
Context context;
public ConnectionClass(Context ctx) {
this.context=ctx;
}
public String getServerResponse(String urlLink)
{
try
{
HttpClient client = new DefaultHttpClient();
HttpPost http_get = new HttpPost(url);
HttpResponse responses;
responses = client.execute(http_get);
if (responses != null)
{
InputStream in = responses.getEntity().getContent();
String a = convertStreamToString(in);
// Log.i("RETURN",a+"");
return a;
}
}
catch (Exception e)
{
e.printStackTrace();
}
return null;
}
String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line);
}
}
catch (Exception e)
{
//Toast.makeText(context, e.toString()+" io2", Toast.LENGTH_LONG).show();
}
finally
{
try
{
is.close();
}
catch (Exception e)
{
//Toast.makeText(context, e.toString()+" io3", Toast.LENGTH_LONG).show();
}
}
return sb.toString();
}
}
希望它有助于你:)
THX