使用Parse - Android获取推特个人资料图片

时间:2015-03-05 19:13:17

标签: android twitter parse-platform

我一直试图获得Twitter ParseUser的个人资料图片。我使用了ParseTwitterUtils来允许用户登录我的应用程序,并且在注册到我的解析数据库时尝试获取他们的个人资料照片。

我使用了Parse docs中的以下内容:



ParseTwitterUtils.logIn(this, new LogInCallback() {
  @Override
  public void done(ParseUser user, ParseException err) {
    if (user == null) {
      Log.d("MyApp", "Uh oh. The user cancelled the Twitter login.");
    } else if (user.isNew()) {
      Log.d("MyApp", "User signed up and logged in through Twitter!"); 
          ParseTwitterUtils.getTwitter().getUserId();
		  user.setUsername(ParseTwitterUtils.getTwitter()
                .getScreenName());
              
		  user.saveInBackground();
		  makeTwitterRequest();
		  MyActivity();
    } else {
      Log.d("MyApp", "User logged in through Twitter!");
    }
  }
});




登录工作绝对正常,并且用户名很好。这是我的makeTwitterRequest()方法,目的是尝试获取配置文件图片。我还没有尝试将它存储到Parse,只是为了使用Twitter API调用来获取它:



private void makeTwitterRequest() {
    	

    	Twitter twitter = ParseTwitterUtils.getTwitter();
    	String twitterUrl = "https://api.twitter.com/1.1/users/show.json?user_id=" + twitter.getUserId();
    	HttpClient client = new DefaultHttpClient();
    	HttpGet request = new HttpGet(twitterUrl);
    	twitter.signRequest(request);
		try {
			HttpResponse response = client.execute(request);
			
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}




运行此功能后,每当我点击Twitter登录按钮并登录时,我都会收到错误消息:



03-05 19:06:56.076: E/AndroidHttpClient(15055): Leak found
03-05 19:06:56.076: E/AndroidHttpClient(15055): java.lang.IllegalStateException: AndroidHttpClient created and never closed
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.net.http.AndroidHttpClient.<init>(AndroidHttpClient.java:158)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.net.http.AndroidHttpClient.newInstance(AndroidHttpClient.java:144)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.twitter.Twitter.authorize(Twitter.java:115)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.TwitterAuthenticationProvider.authenticate(TwitterAuthenticationProvider.java:39)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.ParseUser.authenticateAsync(ParseUser.java:1482)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.ParseUser.logInWithAsync(ParseUser.java:1502)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.ParseUser.logInWithAsync(ParseUser.java:1291)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.ParseTwitterUtils.logInInBackground(ParseTwitterUtils.java:247)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.parse.ParseTwitterUtils.logIn(ParseTwitterUtils.java:264)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at <mypackagename>.SignUpOrLoginActivity.onTwitterLoginButtonClicked(SignUpOrLoginActivity.java:231)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at <mypackagename>.SignUpOrLoginActivity.access$2(SignUpOrLoginActivity.java:229)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at <mypackagename>.SignUpOrLoginActivity$5.onClick(SignUpOrLoginActivity.java:95)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.view.View.performClick(View.java:4756)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.view.View$PerformClick.run(View.java:19749)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.os.Handler.handleCallback(Handler.java:739)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.os.Handler.dispatchMessage(Handler.java:95)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.os.Looper.loop(Looper.java:135)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at android.app.ActivityThread.main(ActivityThread.java:5221)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at java.lang.reflect.Method.invoke(Native Method)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at java.lang.reflect.Method.invoke(Method.java:372)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-05 19:06:56.076: E/AndroidHttpClient(15055): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
&#13;
&#13;
&#13;

我相信Parse应该通过登录程序自动排除,但有人知道出了什么问题吗?非常感谢你的时间!

1 个答案:

答案 0 :(得分:3)

您可以使用以下代码段:

Twitter twitter = ParseTwitterUtils.getTwitter();

HttpGet verifyGet = new HttpGet("https://api.twitter.com/1.1/users/show.json?user_id=" + twitter.getUserId());
ParseTwitterUtils.getTwitter().signRequest(verifyGet);

OkHttpClient client = new OkHttpClient();

Request request = null;

try {
    request = new Request.Builder()
            .url(verifyGet.getURI().toURL())
            .addHeader("Authorization", verifyGet.getFirstHeader("Authorization").getValue())
            .build();
} catch (MalformedURLException e) {
    e.printStackTrace();
}

client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Request request, IOException e) {

    }

    @Override
    public void onResponse(Response response) throws IOException {
        if (response.isSuccessful()) {
            String jsonString = response.body().string();
        }
    }
});

取决于OkHttp by Square(https://github.com/square/okhttp