0

I'm creating an android application in which i'm using Facebook connectivity.I used this tutorial for reference.But when i tried to parse json,it gave me NetworkOnMainThreadException.I dont exactly know how to use thread and AsynTask properly.Somebody please help me out with fixing this...

Here is my class...

 public class MainActivity extends Activity implements OnClickListener  {

    Button btn;
    Facebook fb;
    ImageView loginButton,profilePicture;
    TextView username;
    Bitmap bmp;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fb = new Facebook("xxxxxx");
        sp = getPreferences(MODE_PRIVATE);
        String access_token = sp.getString("access_token", null);
        long expires = sp.getLong("access_expires", 0);

        if(access_token !=null)
        {
            fb.setAccessToken(access_token);
        }
        if(expires !=0)
        {
            fb.setAccessExpires(expires);
        }

        loginButton = (ImageView) findViewById(R.id.btn_login);
        username = (TextView) findViewById(R.id.txt_username);
        profilePicture = (ImageView) findViewById(R.id.img_profpic);
        loginButton.setOnClickListener(this);

    }

     public void onClick(View view) 
     {

         if(fb.isSessionValid())
            {
                // logout
                try {
                    fb.logout(getApplicationContext());
                     new GetProfile().execute("");

                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            else
            {
                //login
                fb.authorize(MainActivity.this,new String[] {"email"},new DialogListener() {

                    @Override
                    public void onFacebookError(FacebookError e) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this,  "MSG: "+e.getMessage()+ " CODE: "+e.getErrorCode(), Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onError(DialogError e) {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, "onError", Toast.LENGTH_SHORT).show();

                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // TODO Auto-generated method stub
                        Editor editor = sp.edit();
                        editor.putString("access_token", fb.getAccessToken());
                        editor.putLong("access_expires", fb.getAccessExpires());
                        editor.commit();
                        //new GetProfile().execute("");
                    }

                    @Override
                    public void onCancel() {
                        // TODO Auto-generated method stub
                        Toast.makeText(MainActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
                    }
                });

            }

            }

     private class GetProfile extends AsyncTask<String, Void, String> 
     {

         @Override
            protected String doInBackground(String... params) 
            {
             if(fb.isSessionValid())
            {
                loginButton.setImageResource(R.drawable.fb_logout);
                profilePicture.setVisibility(ImageView.VISIBLE);

                JSONObject obj = null;
                URL img_url = null;


                try {
                    String jsonUSer = fb.request("me");
                    obj = Util.parseJson(jsonUSer);

                    String id = obj.optString("id");
                    String name = obj.optString("name");

                    img_url = new URL("http://graph.facebook.com/"+id+"/picture?type=small");
                    bmp = BitmapFactory.decodeStream(img_url.openConnection().getInputStream());

                } 
                catch (FacebookError e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
                return "Executed";
            }

            @Override
            protected void onPostExecute(String name) 
            {
                TextView txt = (TextView) findViewById(R.id.txt_username);

                username.setText("Welcome, "+name);

                profilePicture.setImageBitmap(bmp);

                // might want to change "executed" for the returned string passed
                // into onPostExecute() but that is upto you
            }

            @Override
            protected void onPreExecute() {}

            @Override
            protected void onProgressUpdate(Void... values) {}
        }
Nevaeh
  • 1,519
  • 7
  • 24
  • 45
  • 2
    "I dont exactly know how to use thread and AsynTask properly" -- what have you tried, and what specific problems did you encounter? – CommonsWare Oct 04 '14 at 14:05
  • Please see the updated Question.I dont really understand what is giving me error.Please help... – Nevaeh Oct 04 '14 at 16:38

0 Answers0