Stack Exchange
Using facebook sdk for post data in server
I want send facebook some information data to send server. I am using localhost(10.0.2.2) xampp. But when I run this code then it's crashed. please tell me. Get request is doing work accurately but when i use post request then it's crashed. plz tell me rapidly.
package com.example.facebookfirstapp;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
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.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Browser.BookmarkColumns;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.ProfilePictureView;

public class MainActivity extends Activity {
    private ProfilePictureView profilePictureView;
    private ImageView img;
    public static final int SUCCESS = 1;
    public static final int FAILURE = 0;
    private ProgressDialog pd;
    // Has Key==> xWs/zQ8hjgnGgamXM8LgP/T/8Mw=

    ArrayList<Data> datalist;
    public static final String REQ_URL = "http://10.0.2.2/DataParseTest/getalldata.php";
    public static final String POST_URL="http://10.0.2.2/DataParseTest/adddata.php";
    String name;
    String userid;
    String username;
    String email;
    ListView lv;
    LoginData ld;
    ArrayAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        profilePictureView = (ProfilePictureView) findViewById(R.id.selection_profile_pic);
        // img=(ImageView)findViewById(R.id.selection_profile_pic);
        lv=(ListView)findViewById(R.id.lvData);

        // start Facebook Login
        Session.openActiveSession(this, true, new Session.StatusCallback() {

            // callback when session changes state
            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {
                if (session.isOpened()) {

                    // make request to the /me API
                    Request.newMeRequest(session,
                            new Request.GraphUserCallback() {

                                // callback after Graph API response with user
                                // object
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
                                        TextView welcome = (TextView) findViewById(R.id.welcome);
                                        welcome.setText(user.getName() + "!");
                                        profilePictureView.setProfileId(user
                                                .getId());
                                        // user.asMap().get("email")
                                        // ImageView fbImage = ( ( ImageView)
                                        // profilePictureView.getChildAt(0));
                                        // Bitmap bitmap = ( ( BitmapDrawable)
                                        // fbImage.getDrawable()).getBitmap();
                                        // img.setImageBitmap(bitmap);


                                         name=user.getName();
                                         userid=user.getId();
                                         username=user.getUsername();
                                         email=user.asMap().get("email").toString();
                                             ld=new LoginData(name, username, email, userid);






                                        //-----------------------------GET REQUEST------------------------------------

//                                      if (isNetworkAvailabe()) {
//                                          // pd=ProgressDialog.show(getApplicationContext(), "", "Fetching Data..",false,true);
//                                          
//                                         
//                                      }

                                        //---------------------------Post REQUEST-------------------------
                                        if(isNetworkAvailabe()){
                                            //Log.e("Login data", ld.getEmail()+" "+ld.getName()+" "+ld.getUsername()+" "+ld.getUserid());

                                            PostThread pt=new PostThread(ld);
                                            pt.start();
                                            RequestThread t=new RequestThread();
                                                t.start();

                                        }


                                    }
                                }
                            }).executeAsync();
                }

                // --------------Log out Facebook--------------
                // session.closeAndClearTokenInformation();
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Session.getActiveSession().onActivityResult(this, requestCode,
                resultCode, data);
    }





    //----------------------------------POST REQUEST--------------------------------------
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
        //  pd.dismiss();
            switch (msg.what) {
            case SUCCESS:


                adapter=new ArrayAdapter(getApplicationContext(), R.layout.layoutfont, datalist);
                lv.setAdapter(adapter);
//           for (Data data : datalist ) {
//              Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_LONG).show();
//          }   

                break;
            case FAILURE:

                break;

            default:
                break;
            }

        };
    };

    class PostThread extends Thread{
        private LoginData ld;
        public PostThread(LoginData ld){
            this.ld=ld;
        }
        @Override
        public void run() {
           DefaultHttpClient client=new DefaultHttpClient();
           HttpPost postReq=new HttpPost(POST_URL);
           List params=new ArrayList<BasicNameValuePair>();
           params.add(new BasicNameValuePair("name", ld.getName()));
           params.add(new BasicNameValuePair("username", ld.getUsername()));
           params.add(new BasicNameValuePair("email", ld.getEmail()));
           params.add(new BasicNameValuePair("userid",ld.userid));


           try {
            postReq.setEntity(new UrlEncodedFormEntity(params));
            HttpResponse resp=client.execute(postReq);
            if(resp.getStatusLine().getStatusCode()==HttpStatus.SC_OK){
                HttpEntity entity=resp.getEntity();
                String jsonStr=EntityUtils.toString(entity);
                Log.e("JSON Response", jsonStr);
                JSONObject jsonObj=new JSONObject(jsonStr);
                int success=jsonObj.getInt("success");
                if(success==1){
                    postHandler.sendEmptyMessage(success);
                }else{
                    postHandler.sendEmptyMessage(FAILURE); 
                } 


            }else{

            }

           } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            postHandler.sendEmptyMessage(FAILURE);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            postHandler.sendEmptyMessage(FAILURE);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            postHandler.sendEmptyMessage(FAILURE);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            postHandler.sendEmptyMessage(FAILURE);
        }



        }

    }


    Handler postHandler=new Handler(){
        public void handleMessage(android.os.Message msg) {
            pd.dismiss();
            if(msg.what==SUCCESS) 
                {
                    Toast.makeText(getApplicationContext(), "Book added Successfully", Toast.LENGTH_LONG).show();
                }else{
                    Toast.makeText(getApplicationContext(), "Book added Failed", Toast.LENGTH_LONG).show();
                }
        };
    };

Error:
10-22 05:41:39.207: W/dalvikvm(2389): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
    10-22 05:41:39.227: E/AndroidRuntime(2389): FATAL EXCEPTION: main
    10-22 05:41:39.227: E/AndroidRuntime(2389): java.lang.NullPointerException
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at com.example.facebookfirstapp.MainActivity$3$1.onCompleted(MainActivity.java:101)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at com.facebook.Request$1.onCompleted(Request.java:281)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at com.facebook.Request$4.run(Request.java:1666)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at android.os.Handler.handleCallback(Handler.java:725)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at android.os.Handler.dispatchMessage(Handler.java:92)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at android.os.Looper.loop(Looper.java:137)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at android.app.ActivityThread.main(ActivityThread.java:5041)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at java.lang.reflect.Method.invokeNative(Native Method)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at java.lang.reflect.Method.invoke(Method.java:511)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    10-22 05:41:39.227: E/AndroidRuntime(2389):     at dalvik.system.NativeStart.main(Native Method)
    10-22 05:41:44.377: I/Choreographer(2599): Skipped 40 frames!  The application may be doing too much work on its main thread.
    10-22 05:41:44.387: D/gralloc_goldfish(2599): Emulator without GPU emulation detected.


Can you please post the stacktrace of the error. Also does it occur on the server or in your program?

sure, I update it. Thanks
