试试这个:Android 演示。愿它有帮助。
if(isConnected()){
OAuthService authService = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(Constants.CONSUMER_KEY)
.apiSecret(Constants.CONSUMER_SECRET)
.callback(Constants.OAUTH_CALLBACK_URL)
.build();
final OAuthRequest request = new OAuthRequest(Verb.POST, "https://upload.twitter.com/1/statuses/update_with_media.json");
verifier = uri.getQueryParameter(Constants.IEXTRA_OAUTH_VERIFIER);
try {
Token token = getToken();
if(token!=null){
authService.signRequest(token, request);
MultipartEntity entity = new MultipartEntity();
entity.addPart("status", new StringBody("Your Status!")); // THIS IS THE TWITTER MESSAGE
entity.addPart("media", new FileBody(new File(uriString))); // THIS IS THE PHOTO TO UPLOAD
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
request.addPayload(out.toByteArray());
request.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
progressDialog = ProgressDialog.show(this,Constants.LABEL_BLANK, "Uploading Image",true);
Thread requestThread = new Thread() {
public void run() {
try {
response = new JSONObject (request.send().getBody());
uiHandler.sendEmptyMessage(0);
}catch (JSONException e) {
Log.e("TOUR_APP_TAG", "JSONException Thrown: " + e.getMessage());
errorHandler.sendEmptyMessage(0);
}
}
};
requestThread.start();
}
...
...
final Handler uiHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(progressDialog != null)progressDialog.dismiss();
if(response.has("id"))
//your code
}
};
final Handler errorHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if(progressDialog != null)progressDialog.dismiss();
//your code
}
};