Issue
I am trying to open up a URL in WebView
but i am unable to do so and i think that is because the session is not maintained.
I am sending the username, password and the id of user to the server in an activity. here's the code ..
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;
}
}
After this if the response from the server side is "1" i am opening a new activity in which I need to display the content of the user in the WebView
here's the code
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");
}
}
but i am unable to load the URL corresponding o the user, I get php errors over the webviewactivity and that is because i am unable to maintain the session of the user who has logged in. Please suggest me some solution to this.
Solution
What you need to do is to save the cookie used by PHP to keep track of your session when it is returned from the post request you are making and use it for the following requests. There are several ways of doing it depending on how you do the post request.
This answer describes how cookie handling could be done: https://stackoverflow.com/a/687453/1525300
Answered By - wojciii
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.