我正在尝试在其中打开一个 URL,WebView
但我无法这样做,我认为这是因为未维护会话。我在活动中将用户名、密码和用户 ID 发送到服务器。这是代码..
public class ServiceActivity extends Activity {
private Button button_back;
private Button button_submit_user_pass;
private EditText edit_id_code;
private String contents;
private String format;
private String username;
private String password;
private String id;
public static HttpClient client;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.qr_code_view);
edit_id_code = (EditText) findViewById(R.id.editText_id);
button_back = (Button) findViewById(R.id.button_back);
button_submit_user_pass = (Button) findViewById(R.id.button_submit_user_pass);
Intent user_pass = getIntent();
username = user_pass.getStringExtra("user");
password = user_pass.getStringExtra("pass");
button_submit_user_pass
.setOnClickListener(user_pass_qr_submit_listener);
button_back.setOnClickListener(back_listener);
}
private View.OnClickListener user_pass_qr_submit_listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
id = edit_id_code.getText().toString();
client = new DefaultHttpClient();
HttpPost post1 = new HttpPost(
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("uname", username));
nvp.add(new BasicNameValuePair("password", password));
nvp.add(new BasicNameValuePair("id", id));
post1.setEntity(new UrlEncodedFormEntity(nvp));
HttpResponse resp = client.execute(post1);
String responseText = inputStreamTOString(
resp.getEntity().getContent()).toString();
Log.i("response", responseText);
int num = Integer.parseInt(responseText);
if (num == 0) {
Toast.makeText(getApplicationContext(),
"Response" + responseText, 0).show();
} else if (num == 1) {
Intent survey = new Intent(ServiceActivity.this,
WebViewActivity.class);
startActivity(survey);
}
} catch (Exception e) {
Log.e("error", "ERROR" + e);
}
}
};
private View.OnClickListener back_listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
};
private StringBuilder inputStreamTOString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
// read response until the end
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is,
"iso-8859-1"), 8);
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
// TODO: handle exception
}
return total;
}
}
在此之后,如果来自服务器端的响应为“1”,我将打开一个新活动,在该活动中我需要在WebView
代码中显示用户的内容
public class WebViewActivity extends Activity{
private WebView web;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
web = (WebView) findViewById(R.id.webView);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("xxxxxxxxxxxxxxxxxxxxxxxxxxxx");
}
}
但我无法加载与用户对应的 URL,我在 webviewactivity 上收到 php 错误,那是因为我无法维护已登录用户的会话。请建议我一些解决方案。