0

我正在尝试在其中打开一个 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 错误,那是因为我无法维护已登录用户的会话。请建议我一些解决方案。

4

2 回答 2

0

您需要做的是保存 PHP 使用的 cookie,以便在您发出的 post 请求返回时跟踪您的会话,并将其用于以下请求。有几种方法可以做到这一点,具体取决于您如何执行发布请求。

此答案描述了如何完成 cookie 处理:https ://stackoverflow.com/a/687453/1525300

于 2012-09-16T08:59:34.230 回答
0

尝试使用 SharedPreferences 为您的应用程序保持会话。

为了节省价值。

SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
Editor editor = prefs.edit();
editor.putString("key", value);
editor.commit();

在其他活动中检索

SharedPreferences prefs;
prefs = PreferenceManager.getDefaultSharedPreferences(this);
String variable = prefs.getString("key","default value");
于 2012-09-14T05:18:37.867 回答