Exemplo n.º 1
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);
    screenName = getIntent().getStringExtra("screen_name");

    client = TwitterApplication.getRestClient();
    if (screenName == null || screenName.equals("")) {
      client.getAccountInfo(
          new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
              Log.d("DEBUG", response.toString());
              user = User.fromJSON(response);
              screenName = user.getScreenName();
              // My current user account's info
              getSupportActionBar().setTitle("@" + user.getScreenName());
              populateProfileHeader(user);
            }
          });
    } else {
      client.getUserInfo(
          screenName,
          new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
              Log.d("DEBUG", response.toString());
              try {
                user = User.fromJSON(response.getJSONObject(0));
                screenName = user.getScreenName();
                // My current user account's info
                getSupportActionBar().setTitle("@" + user.getScreenName());
                populateProfileHeader(user);
              } catch (JSONException e) {
                e.printStackTrace();
              }
            }
          });
    }

    if (savedInstanceState == null) {
      if (screenName == null || screenName.equals("")) screenName = "TaogJiang";
      UserTimelineFragment fragmentUserTimeline = UserTimelineFragment.newInstance(screenName);
      FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
      ft.replace(R.id.flContainer, fragmentUserTimeline);
      ft.commit();
    }
  }
  public void onPostTweet(View v) {
    final Context c = this;

    EditText etPost = (EditText) findViewById(R.id.etPost);
    String q = etPost.getText().toString();
    /*
    try {
        q = URLEncoder.encode(q, "UTF-8").replace("+", "%20");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    */

    JsonHttpResponseHandler j =
        new JsonHttpResponseHandler() {
          public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            Toast.makeText(c, "Post Success", Toast.LENGTH_SHORT).show();

            Intent result = new Intent();
            result.putExtra("isSuccess", "success");
            setResult(RESULT_OK, result);
            finish();
          }

          @Override
          public void onFailure(
              int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            Toast.makeText(c, "Post Failure", Toast.LENGTH_SHORT).show();

            Intent result = new Intent();
            result.putExtra("isSuccess", "failure");
            setResult(RESULT_OK, result);
            finish();
          }
        };

    try {
      client.postMyTweet(q, j);
    } catch (Exception e) {
      e.printStackTrace();
      Log.d("EXcpetion: !!!", "");
    }
  }
  // Send api request to get the timeline JSON
  //
  public void populateTimeline() {
    client.getHomeTimeline(
        oldestTweetId,
        new JsonHttpResponseHandler() {
          @Override
          public void onSuccess(int statusCode, Header[] headers, JSONArray json) {
            Log.d("DEBUG", json.toString());
            addAll(Tweet.fromJSONArray(json));
            oldestTweetId += tweets.get(tweets.size() - 1).getId();
          }

          @Override
          public void onFailure(
              int statusCode, Header[] headers, Throwable throwable, JSONObject errorResp) {
            Log.d("DEBUG", errorResp.toString());
            Toast.makeText(getActivity(), "Request tweets failed", Toast.LENGTH_LONG).show();
          }
        });
  }
  public void onTweet(MenuItem mi) {
    String Tweet = et_message.getText().toString();
    if (!Tweet.equals("")) {

      Long tsLong = System.currentTimeMillis() / 1000;
      String ts = tsLong.toString();
      Intent i = new Intent();

      client.postNewTweet(
          Tweet,
          ts,
          new JsonHttpResponseHandler() {

            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject json) {
              tweet composedTweet = tweet.fromJson(json);
              et_message.setText("");
              /*status=test&in_reply_to=cs480test2&oauth_version=1.0&oauth_nonce=XXXXXX&
              oauth_timestamp=1411668337&oauth_consumer_key=XXXXXXX&oauth_token=XXXXXXXX&
              oauth_signature_method=HMAC-SHA1&oauth_signature=XXXXX*/
              Intent i = new Intent();
              i.putExtra("composedTweet", composedTweet);
              // Package with result.
              setResult(RESULT_OK, i);
              // Dismiss the activity.
              finish();
            }

            @Override
            public void onFailure(
                int statusCode, Header[] headers, Throwable throwable, JSONArray e) {
              Log.d("debug", e.toString());
              Log.d("debug", throwable.toString());
            }
          });
    } else {
      Toast.makeText(this, "No Tweet Submitted", Toast.LENGTH_SHORT).show();
    }
  }
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_compose_activity);
    client = new TwitterApp().getRestClient();
    //       btn_save = (Button) findViewById(R.id.btn_tweet);
    et_message = (EditText) findViewById(R.id.et_message);

    client.getCurrentUser(
        new JsonHttpResponseHandler() {
          @Override
          public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            currentUser = new User();
            currentUser = User.fromJson(response);
            screenName = currentUser.getScreenName();
            //               Toast.makeText(getApplicationContext(), screenName,
            // Toast.LENGTH_SHORT).show();

            tv_user = (TextView) findViewById(R.id.tv_user);
            iv_profilePic = (ImageView) findViewById(R.id.iv_profilePic);

            tv_user.setText(currentUser.getScreenName());
            iv_profilePic.setImageResource(
                android.R.color.transparent); // clear image for recycled view
            Picasso.with(getApplicationContext())
                .load(currentUser.getProfileImageUrl())
                .into(iv_profilePic);
          }

          @Override
          public void onFailure(
              int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
          }
        });
  }