How to make mobile client for nirvana? Example for android.
Developers blog
Example in Java:
Lang:
package app.nirvana.Agent;

import android.app.Activity;
import android.content.*;
import android.graphics.Bitmap;
import android.os.*;
import android.view.*;
import android.webkit.*;
import org.json.*;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;


public class AgentActivity extends Activity {
private AgentActivity _ = this;
public WebView wv;

private String app_id         = "{ваш_app_id}";
private String api_secret     = "{ваш_api_secret}";
private String auth_url       = "http://nirvana.fm/api/auth";
private String success_url    = "http://url, on which will be redirected in case of successful user authorization";
private String login_form_url = "";

private JSONArray contextMenu = null;


@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);

if (_.contextMenu == null) return true;

menu.clear();
try {
for (int i = 0; i < _.contextMenu.length(); i++) {
menu.add(0, _.contextMenu.optJSONObject(i).getInt("id"), 0, _.contextMenu.optJSONObject(i).getString("title"));
}

} catch (JSONException e) {
System.out.println(e.toString());
}

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)  {
int id = item.getItemId();

try {
for (int i = 0; i < _.contextMenu.length(); i++) {
if (_.contextMenu.optJSONObject(i).getInt("id") == id) {
_.execJS("MobileClient." + _.contextMenu.optJSONObject(i).getString("func") + "()");
}
}

} catch (JSONException e) {
System.out.println(e.toString());
}

return true;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.agent_activity);

try {
_.login_form_url = _.auth_url + "?app_id=" + _.app_id + "&api_secret=" + _.api_secret + "&success_url=" + URLEncoder.encode(_.success_url, "UTF-8");

} catch (UnsupportedEncodingException e) {
System.out.println("URLEncoder.encode fail");
}


_.wv = (WebView)findViewById(R.id.wv);
CookieSyncManager.createInstance(_);

WebSettings webSettings = _.wv.getSettings();
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setBuiltInZoomControls(false);

_.wv.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
System.out.println("AgentActivity.wv onReceivedError errorCode = " + Integer.toString(errorCode) + ", description = " + description + ", failingUrl = " + failingUrl);
}

public void onPageFinished(WebView view, String url) {
CookieSyncManager.getInstance().sync();

System.out.println("onPageFinished " + url);
if (url.indexOf(_.success_url) == 0) {
_.js_init();
}
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
System.out.println("shouldOverrideUrlLoading " + url);
if (url.indexOf(_.success_url) == 0) {
System.out.println("wv success url = " + url);
return false;
}
return true;
}
});

_.wv.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
String prefix = "MobileClient.";

if (!message.startsWith(prefix)) {
return super.onJsPrompt(view, url, message, defaultValue, result);
}

String method_name = "js_" + message.substring(prefix.length());

System.out.println("method_name = " + method_name + ", default_value = " + defaultValue);

String json_result = null;

try {
JSONArray params = new JSONArray(defaultValue);
int len = params.length();
Object []paramValues = new Object[len];
for (int i = 0; i < len; ++i) {
paramValues[i] = params.opt(i);
}

for (Method method : AgentActivity.class.getMethods()) {
if (method.getName().equals(method_name)) {
json_result = (String)method.invoke(_, paramValues);
break;
}
}

} catch (JSONException e) {
} catch (InvocationTargetException e) {
} catch (IllegalAccessException e) {
}

result.confirm(json_result);

return true;
}
});

_.wv.loadUrl(_.login_form_url);
}


public void execJS(String code) {
_.wv.loadUrl("javascript:(function(){" + code + "})()");
}

@Override
protected void onResume() {
super.onResume();
CookieSyncManager.getInstance().startSync();
}

@Override
protected void onPause() {
super.onPause();
CookieSyncManager.getInstance().stopSync();
}


public void js_init() {
_.execJS(
"MobileClient.init({ " +
"system: 'android', " +
"success_url: '" + _.success_url + "', " +
"login_form_url: '" + _.login_form_url + "'" +
" })"
);
}


public void js_log(String s) {
System.out.println("AgentActivity.js_log |" + s + "|");
}

public void js_setMenu(JSONArray menu) {
_.contextMenu = menu;
}
}


The success_url script, in my case, is an empty html page in which the class is loaded MobileClient.class.js
Lang:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<script src="MobileClient.class.js"></script>
</head>
<body>
</body>
</html>


Click below to see a continuation →
5
0See full →  
What changes in API for develop mobile nirvana client
Developers blog
What changes?
  • /_auth -> /auth - removed parameter "error_url",  now when the user incorrectly enters the username and password, the api itself displays a message about the error, the developer no longer needs to show this message himself. There was only "success_url" to which redirection occurs from the login and password input form in case of successful authorization. Also, this method no longer returns the result as xml, only html.
  • /_contacts -> /pm/contacts - now it immediately displays a list of folders and a list of contacts, otherwise you had to make 2 api calls in a row.
  • /_messages -> /pm/messages
  • /_send_message -> /pm/send_message
  • The <user> field now in all methods where there is information about the user’s profile gives the same data structure, earlier some fields might not coincide.
3
0
We create group for application developers
Developers blog
Group was created for application developers.

There will be:
  • Information about new API, discussions about api, bugs, testing, suggestions for improvement API.
  • Communication between developers, sharing experience, giving advice, suggesting ideas.
  • We post versions of programs for testing.
  • IT humor and just talk about different things.
3
17
0