sharedpreferences and http post , give back force close ...

Put your problem here if it does not fit any of the other categories.

sharedpreferences and http post , give back force close ...

Postby cyb3r » Sat Apr 09, 2011 8:09 pm


the code is about doing a login process and if it was successful ,
then a boolean "logged_in" will get set in sharedpreferences("su")

but it gives me force closing...

i dont know where's the problem
the code is here:

Code: Select all
package com.httptester.joonemadaretkarkon;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.ByteArrayBuffer;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class mainactivity extends Activity {
   TextView tv;
   String text;
   Button button;
   // create a reference to the shared preferences object

   // obtain an editor to add data to my SharedPreferences object

   /** Called when the activity is first created. */
   public void onCreate(Bundle savedInstanceState) {
       SharedPreferences settings = getSharedPreferences("su", 0);

       boolean logged_in = settings.getBoolean("logged_in", false);
       tv = (TextView)findViewById(;
               tv.setText("you logged in before");

               tv.setText("you're not logged in before");


       Button button = (Button) findViewById(;

       text = "";
       button.setOnClickListener(new OnClickListener() {
           public void onClick(View v) {


   public void postData(){
       String stdnum,pwd;
       EditText stdnum2 = (EditText)findViewById(;
               EditText pwd2 = (EditText)findViewById(;
               stdnum = stdnum2.getText().toString();
               pwd = pwd2.getText().toString();
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("
       try {
               // Add your data
           List<NameValuePair> nameValuePairs = new
           nameValuePairs.add(new BasicNameValuePair("username",
           nameValuePairs.add(new BasicNameValuePair("passwd", pwd));

           // Execute HTTP Post Request
           HttpResponse response = httpclient.execute(httppost);

           InputStream is = response.getEntity().getContent();
           BufferedInputStream bis = new BufferedInputStream(is);
           ByteArrayBuffer baf = new ByteArrayBuffer(20);

            int current = 0;
            while((current = != -1){

           /* Convert the Bytes read to a String. */
           text = new String(baf.toByteArray());
               SharedPreferences settings =
getSharedPreferences("su", 0);
               SharedPreferences.Editor editor = settings.edit();
               editor.putBoolean("logged_in", true);

               // Commit the edits!

               tv.setText("welcome, now u r permanently logged in ");
           else if (text.equals("no"))

   } catch (ClientProtocolException e) {
           // TODO Auto-generated catch block
   } catch (IOException e) {
           // TODO Auto-generated catch block

what should i do ?:(
Once Poster
Once Poster
Posts: 1
Joined: Mon Mar 28, 2011 3:29 pm


Re: sharedpreferences and http post , give back force close

Postby imbroken » Sun Apr 10, 2011 11:30 am

you could start by posting your LogCat :)
Senior Developer
Senior Developer
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests