Here it is a complete Activity class that manage with the Viadeo SDK. It uses all features delivered by the SDK. This code doesn’t use XML layout to copy and paste it easily in your project.
package com.viadeo.android.sample;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.viadeo.android.sdk.Viadeo;
import com.viadeo.android.sdk.ViadeoAPIManager.Method;
import com.viadeo.android.sdk.ViadeoConnectButton;
import com.viadeo.android.sdk.ViadeoOAuthListener;
import com.viadeo.android.sdk.ViadeoRequest;
import com.viadeo.android.sdk.ViadeoRequestListener;
import com.viadeo.android.sdk.ViadeoRequester;
public class ViadeoSampleAppActivity extends Activity implements ViadeoRequestListener {
private static final String VIADEO_CLIENT_ID = "azertyuiop";
private static final String VIADEO_CLIENT_SECRET = "qsdfghjklm";
private static final int REQUEST_CODE_ME = 789546;
private static final int REQUEST_CODE_UPDATE_STATUS = 125863;
private Viadeo _viadeo;
private ProgressBar _progressBar;
private TextView _textView;
private ViadeoConnectButton _button;
private ScrollView _scrollView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_scrollView = new ScrollView(this);
setContentView(_scrollView);
buildUI();
initViadeo();
}
private void buildUI() {
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
_scrollView.addView(linearLayout);
_button = new ViadeoConnectButton(this);
linearLayout.addView(_button, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
_progressBar = new ProgressBar(this);
_progressBar.setVisibility(View.INVISIBLE);
linearLayout.addView(_progressBar, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
_textView = new TextView(this);
linearLayout.addView(_textView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
}
private void initViadeo() {
_viadeo = new Viadeo(this, VIADEO_CLIENT_ID, VIADEO_CLIENT_SECRET);
_button.refreshState(_viadeo);
if(_viadeo.isLoggedIn()) {
getMe();
}
_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(!_viadeo.isLoggedIn()) {
_viadeo.authorize(new ViadeoOAuthListener() {
@Override
public void onViadeoOAuthComplete() {
_button.refreshState(_viadeo);
getMe();
}
@Override
public void onViadeoOAuthCancel() {
}
@Override
public void onViadeoOAuthError(int errorCode, String description, String failingUrl) {
}
});
} else {
_viadeo.logOut();
_button.refreshState(_viadeo);
_textView.setText("");
}
}
});
}
private void getMe() {
_progressBar.setVisibility(View.VISIBLE);
ViadeoRequest viadeoRequest = new ViadeoRequest(REQUEST_CODE_ME, "/me", null, Method.GET, _viadeo, this);
new ViadeoRequester().execute(viadeoRequest);
updateStatus();
}
private void updateStatus() {
Bundle params = new Bundle();
params.putString("message", "hello from Android Viadeo SDK");
ViadeoRequest viadeoRequest = new ViadeoRequest(REQUEST_CODE_UPDATE_STATUS, "/status", params, Method.POST, _viadeo, this);
new ViadeoRequester().execute(viadeoRequest);
}
@Override
public void onViadeoRequestComplete(int requestCode, String response) {
_progressBar.setVisibility(View.GONE);
switch (requestCode) {
case REQUEST_CODE_ME:
try {
String me = new String();
JSONObject json = new JSONObject(response);
me += "my Viadeo API id : " + json.getString("id") + "\n";
me += "my Viadeo API link : " + json.getString("link") + "\n";
me += "my name : " + json.getString("name") + "\n";
me += "my number of contacts : " + json.getString("contact_count") + "\n";
me += "my living area : " + json.getJSONObject("location").getString("area") + "\n";
_textView.setText(me);
} catch (JSONException e) {
}
break;
case REQUEST_CODE_UPDATE_STATUS:
break;
default:
break;
}
}
@Override
public void onViadeoRequestError(int requestCode, final String errorMessage) {
_progressBar.setVisibility(View.GONE);
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show();
}
}