我目前正在开发一种社交媒体应用程序,该应用程序具有编写推文并将其发布到Twitter的功能。我进行了一些研究,并在这里找到一个示例:https://github.com/twitter/twitter-kit-android/wiki/Compose-Tweets。调用postToTwitter时,Twitter活动开始,并且文本和图像显示在Twitter UI中,但是当我选择“ tweet”时,没有任何内容发布到Twitter。我已经完成了实现Twitter登录的步骤。我把我的课放在下面了。方法postToDB和postToFacebook与该问题无关,因此可以忽略。
public class CreatePost extends Activity {
private String TAG = "CreatePost";
private Button share;
private EditText description;
private Button pickImage;
private ImageView imageView;
private ProgressBar progressBar;
private CheckBox checkBoxButton;
private CheckBox twitterCheckBox;
private BottomNavigationView navigation;
private Bitmap bitmap = null;
private Post post;
private Uri uri;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch(menuItem.getItemId()){
case R.id.navHome:
Intent i = new Intent(CreatePost.this, MainActivity.class);
startActivity(i);
return true;
case R.id.createPost:
return true;
case R.id.account:
i = new Intent(CreatePost.this, AccountSettings.class);
startActivity(i);
return true;
}
return false;
}
};
private int PICK_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_post);
findViews();
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
navigation.setSelectedItemId(R.id.createPost);
progressBar.setVisibility(View.INVISIBLE);
pickImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(i, "Select Picture"), PICK_IMAGE);
}
});
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(description.getText().length() == 0) {
Toast t = Toast.makeText(getApplicationContext(), "Description cannot be blank", Toast.LENGTH_SHORT);
t.show();
return;
}
post = new Post.PostBuilder()
.addName(User.getCurrentUser().getName())
.addSource(getString(R.string.app_name))
.addDescription(description.getText().toString())
.addImageURL("https://s3.amazonaws.com/media.mediapost.com/dam/cropped/2017/11/24/laughingemoji-560.JPEG")
.addTimePosted(Util.getCurTimeString())
.build();
postToDB();
if(twitterCheckBox.isChecked()){
postToTwitter();
}
if(checkBoxButton.isChecked()){
postToFacebook();
}
}
});
}
private void postToFacebook() {
Log.d(TAG, "postToFacebook called");
SharePhoto photo;
SharePhotoContent content;
ShareDialog sd = new ShareDialog(this);
if (bitmap != null) {
photo = new SharePhoto.Builder()
.setBitmap(bitmap)
.build();
content = new SharePhotoContent.Builder()
.addPhoto(photo)
.build();
sd.show(content);
}
}
private void postToTwitter(){
final TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession();
final Intent intent = new ComposerActivity.Builder(CreatePost.this)
.session(session)
.image(uri)
.text(description.getText().toString())
.createIntent();
startActivity(intent);
}
private void postToDB(){
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url = new StringBuilder()
.append("http://proj309-mg-01.misc.iastate.edu:8080/users/")
.append(User.getCurrentUser().getId())
.append("/newpost")
.toString();
JSONObject payload = null;
try {
payload = new JSONObject()
.put("description", post.getDescription())
.put("timeposted", post.getTimePosted())
.put("imageURL", post.getImageURL());
} catch (JSONException e) {
e.printStackTrace();
}
JsonObjectRequest postRequest = new JsonObjectRequest(url , payload,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
if(checkBoxButton.isChecked()){
Variables.getInstance().addFBPost(post);
}
//Variables.getInstance().getAdapter().notifyDataSetChanged();
//Intent i = new Intent(getApplicationContext(), MainActivity.class);
//startActivity(i);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
Toast t = Toast.makeText(getApplicationContext(),"Server Error, check log", Toast.LENGTH_SHORT);
t.show();
}
}
);
queue.add(postRequest);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView.setImageBitmap(bitmap);
imageView.setVisibility(View.VISIBLE);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void findViews(){
imageView = findViewById(R.id.imageView);
navigation = findViewById(R.id.navigation);
checkBoxButton = findViewById(R.id.post_to_facebook_toggle);
twitterCheckBox = findViewById(R.id.post_to_twitter_toggle);
share = findViewById(R.id.share_button);
description = findViewById(R.id.description);
progressBar = findViewById(R.id.progressBar);
pickImage = findViewById(R.id.imageSelect);
}
}