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

hi

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.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
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.app.Activity;
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. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       SharedPreferences settings = getSharedPreferences("su", 0);

       boolean logged_in = settings.getBoolean("logged_in", false);
       tv = (TextView)findViewById(R.id.textview);
       if(logged_in==true)
       {
               tv.setText("you logged in before");
               finish();

       }
       else
       {
               setContentView(R.layout.main);
               tv.setText("you're not logged in before");
       }



       //tv.setText("logged_in");

       Button button = (Button) findViewById(R.id.login_b);

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


   });}


   public void postData(){
       String stdnum,pwd;
       EditText stdnum2 = (EditText)findViewById(R.id.username);
               EditText pwd2 = (EditText)findViewById(R.id.password);
               stdnum = stdnum2.getText().toString();
               pwd = pwd2.getText().toString();
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost("http://192.168.1.3/scores/
login.php");
       try {
               // Add your data
           List<NameValuePair> nameValuePairs = new
ArrayList<NameValuePair>(1);
           nameValuePairs.add(new BasicNameValuePair("username",
stdnum));
           nameValuePairs.add(new BasicNameValuePair("passwd", pwd));
           httppost.setEntity(new
UrlEncodedFormEntity(nameValuePairs));

           // 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 = bis.read()) != -1){
                   baf.append((byte)current);
            }

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

               // Commit the edits!
               editor.commit();



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


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


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

Top

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 :)
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Exabot [Bot] and 22 guests