我试过使用这个jar的twitter4j-core-2.1.11.jar,发布图片很好。但现在我使用了apache-mime4j-0.6.jar,httpmime-4.0.1.jar,scribe-1.3.3.jar。 Login and posting tweet as text its working
。但我want to post image with text message
我怎么发布它...
i dont want to use twitter4j-core-2.1.11.jar
我想使用https://api.twitter.com/1.1/statuses/update_with_media.json
。请告诉我如何发布图片..
请参阅我的代码。
public class MainActivity extends Activity {
// TWITTER SHARE VARIABLES
final static String APIKEY = "***********";
final static String APISECRET = "*******************************";
final static String CALLBACK = "http://www.google.com";
OAuthService Oathserv;
Token requestToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isInternetAvailable()) {
postStatusOnTwitter();
} else {
Toast.makeText(getApplicationContext(),
"Internet service not available", Toast.LENGTH_LONG)
.show();
}
}
});
}
public boolean isInternetAvailable() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.getActiveNetworkInfo() != null)
return (cm.getActiveNetworkInfo().isConnected() && cm
.getActiveNetworkInfo().isAvailable());
else
return false;
}
// TWITTER SHARING START
private void postStatusOnTwitter() {
final WebView webView1 = (WebView) findViewById(R.id.webView1);
webView1.setVisibility(View.VISIBLE);
webView1.requestFocus(View.FOCUS_DOWN);
webView1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
// set up service and get request token as seen on scribe website
// https://github.com/fernandezpablo85/scribe-java/wiki/Getting-Started
// attach WebViewClient to intercept the callback url
webView1.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// check for our custom callback protocol otherwise use
// default
// behavior
if (url.startsWith("http")) {
// authorization complete hide webview for now.
webView1.setVisibility(View.GONE);
Uri uri = Uri.parse(url);
String verifier = uri.getQueryParameter("oauth_verifier");
if (verifier != null) {
Verifier v = new Verifier(verifier);
// save this token for practical use.
Token accessToken = Oathserv.getAccessToken(requestToken, v);
// host twitter detected from callback
// oauth://twitter
Log.e("Host", uri.getHost().toString());
if (uri.getHost().equals("www.google.com")) {
/*
* OAuthRequest req = new OAuthRequest(Verb.GET,
* "http://api.twitter.com/1/account/verify_credentials.json"
* );
*/
/* OAuthRequest req = new OAuthRequest(Verb.POST,
"https://api.twitter.com/1.1/statuses/update.json");
req.addBodyParameter("status", "Hello Twitter");
Oathserv.signRequest(accessToken, req);
*/
OAuthRequest req = new OAuthRequest(Verb.POST,
"https://api.twitter.com/1.1/statuses/update_with_media.json");
// req.addBodyParameter("status", "bdf Twitter");
req.addBodyParameter("media[]", "http://i.stack.imgur.com/wz0qZ.jpg"); // here will be image
Oathserv.signRequest(accessToken, req);
org.scribe.model.Response response = req.send();
try {
JSONObject json = new JSONObject(response
.getBody());
android.util.Log.i("Twitter Response",
json.toString());
Toast.makeText(getApplicationContext(),
"Shared on twitter successfully",
Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
new Handler().post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
Oathserv = new ServiceBuilder().provider(TwitterApi.class)
.apiKey(APIKEY).apiSecret(APISECRET)
.callback(CALLBACK).build();
requestToken = Oathserv.getRequestToken();
final String authURL = Oathserv.getAuthorizationUrl(requestToken);
// send user to authorization page
webView1.loadUrl(authURL);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
});
}
// TWITTER SHARING END
}
答案 0 :(得分:0)
我解决了twitter post image的问题。
final ProgressDialog progress = UIHelper.showProgress(ShareWostActivity.this, "Twitter !", "Please wait ...");
final WebView webView1 = (WebView) findViewById(R.id.showwost_webView);
webView1.setVisibility(View.VISIBLE);
webView1.requestFocus(View.FOCUS_DOWN);
webView1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
webView1.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, final String url) {
if (url.startsWith("http")) {
// authorization complete hide webview for now.
final Uri uri = Uri.parse(url);
final String verifier = uri.getQueryParameter("oauth_verifier");
Log.i("1", "1-->" + verifier);
if (verifier != null) {
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
Verifier v = new Verifier(verifier);
Token accessToken = s.getAccessToken(requestToken, v);
// host twitter detected from callback
// oauth://twitter
if (uri.getHost().equals("www.google.com")) {
try {
OAuthRequest req = new OAuthRequest(Verb.POST,
"https://api.twitter.com/1.1/statuses/update_with_media.json");
s.signRequest(accessToken, req);
File outputDir = Environment.getExternalStorageDirectory();
File tempFile = File.createTempFile("temp_inner_twitter", ".jpg", outputDir);
Bitmap bitmap = createBitmap();
FileOutputStream fOut = new FileOutputStream(tempFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
Log.i("asds", tempFile.length() + "," + tempFile.getAbsolutePath());
fOut.flush();
fOut.close();
bitmap.recycle();
MultipartEntity entity = new MultipartEntity();
String urii = Uri.parse(createShareLink(urlwost)).toString();
entity.addPart("status", new StringBody("Vote here ww.abc.com\n Download WO? App http://www.google.com/"));
entity.addPart("media", new FileBody(tempFile, "image/jpeg"));
entity.addPart("access_token", new StringBody(accessToken.getToken()));
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
req.addPayload(out.toByteArray());
req.addBodyParameter("status", "I have uploaded test image on twitter");
req.addHeader(entity.getContentType().getName(), entity.getContentType().getValue());
org.scribe.model.Response response = req.send();
JSONObject json = new JSONObject(response.getBody());
Log.i("Twitter Response", json.toString());
if (!json.has("errors")) {
Toast
.makeText(getApplicationContext(), "Image shared on twitter successfully", Toast.LENGTH_LONG)
.show();
} else {
Toast.makeText(getApplicationContext(), "Error while sharing application on twitter",
Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error while sharing application on twitter",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error while sharing application on twitter",
Toast.LENGTH_LONG).show();
}
}
}
}).run();
webView1.setVisibility(View.GONE);
// save this token for practical use.
}
return true;
}
return super.shouldOverrideUrlLoading(view, url);
}
});
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
s = new ServiceBuilder().provider(TwitterApi.class).apiKey(APIKEY).apiSecret(APISECRET).callback(CALLBACK)
.build();
requestToken = s.getRequestToken();
// send user to authorization page
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
final String authURL = s.getAuthorizationUrl(requestToken);
Log.i("2", "--" + authURL);
webView1.loadUrl(authURL);
progress.dismiss();
}
});
}
}).start();