android onSaveInstanceState usage - android

Please guide in the following class what I should save in.
Please remember I am using only one string which is I am retriving from getextra method and there is nothing which I think I should store in the following overridden method.
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
Can you guide me what to store in onsave instance method and what not to?
package com.wozzon.activity;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import com.wozzon.json.JSONService;
import com.wozzon.model.SearchCriteria;
import com.wozzon.pl.UIFactory;
import com.wozzon.util.Util;
import com.wozzon.util.WLConstants;
public class HomeBrowse extends Activity implements OnItemClickListener{
private final static String TAG = HomeBrowse.class.getSimpleName();
private ArrayList<BrowseRow> model=new ArrayList<BrowseRow>();
private BrowseAdapter adapter;
private ListView list;
private TextView browseTitle;
private TextView changeLink;
private SearchCriteria sc =SearchCriteria.getInstance();
private JSONArray jsonArray;
private ProgressDialog m_ProgressDialog = null;
private Runnable progressRunnable;
#Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.browse);
String errorMsg =Util.getExtraValue(getIntent().getExtras(), WLConstants.ERROR_MSG);
list =(ListView)findViewById(R.id.browse_cats);
if(errorMsg!= null){
UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Status", errorMsg);
}
progressRunnable = new Runnable(){
#Override
public void run() {
try {
loadData();
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
}
runOnUiThread(returnRes);
}
};
m_ProgressDialog = ProgressDialog.show(HomeBrowse.this,
"Please wait...", "Loading ...", true);
Thread thread = new Thread(progressRunnable);
thread.start();
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
//UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Error Loading categories", e.getMessage());
}
}
private void loadData()throws Throwable{//loading object
jsonArray = JSONService.getJsonArray(getResources().getString(R.string.catJson));
}
private void fillResultRows(JSONArray jsonArray, BrowseAdapter adapter)throws Throwable{
BrowseRow br = null;
try {
for(int a=0; a<jsonArray.length(); a++){
JSONObject jsonObj = jsonArray.getJSONObject(a);
br = new BrowseRow();
br.name=jsonObj.getString("title");
br.image=jsonObj.getString("image");
br.searchType = jsonObj.getInt("searchType");
if(jsonObj.has("categoryIds")){
br.categoryIds = jsonObj.getString("categoryIds").replaceAll("-", ",");
}
if(jsonObj.has("subCategoryIds")){
br.subCategoryIds = jsonObj.getString("subCategoryIds").replaceAll("-", ",");
}
adapter.add(br);
}
} catch (Throwable e) {
throw e;
}
}
class BrowseAdapter extends ArrayAdapter<BrowseRow> {
BrowseAdapter() {
super(HomeBrowse.this, android.R.layout.simple_list_item_1, model);
}
public View getView(int position, View convertView,
ViewGroup parent) {
View row=convertView;
ResultWrapper wrapper=null;
if (row==null) {
LayoutInflater inflater=getLayoutInflater();
row=inflater.inflate(R.layout.browse_row, null);
wrapper=new ResultWrapper(row);
row.setTag(wrapper);
}
else {
wrapper=(ResultWrapper)row.getTag();
}
wrapper.populateFrom(model.get(position));
return(row);
}
}
class ResultWrapper {
private TextView name;
private String catIds="0";
private String subCatIds="0";
private ImageView image;
private int searchType;
private View row=null;
ResultWrapper(View row) {
this.row=row;
}
void populateFrom(BrowseRow r) {
getName().setText(r.name);
getIcon().setImageResource(getImageIcon(Integer.parseInt(r.image)));
catIds =r.categoryIds;
subCatIds = r.subCategoryIds;
searchType =r.searchType;
}
TextView getName() {
if (name==null) {
name=(TextView)row.findViewById(R.id.browse_row_name);
}
return name;
}
ImageView getIcon() {
if (image==null) {
image=(ImageView)row.findViewById(R.id.browse_row_icon);
}
return image;
}
}
private int getImageIcon(int catId){
int imageSource=R.drawable.all_cats;
switch(catId){
case WLConstants.CATEGORY_FILM: imageSource =R.drawable.film; break;
case WLConstants.CATEGORY_MUSIC: imageSource =R.drawable.music; break;
case WLConstants.CATEGORY_ARTS: imageSource =R.drawable.art; break;
case WLConstants.CATEGORY_KIDS: imageSource =R.drawable.museum; break;
case WLConstants.CATEGORY_GALLERY_MUSEUM: imageSource =R.drawable.kids; break;
case WLConstants.CATEGORY_COMEDY: imageSource =R.drawable.comedy; break;
case WLConstants.CATEGORY_NIGHT_CLUBS: imageSource =R.drawable.clubs; break;
case WLConstants.CATEGORY_ATTRACTION: imageSource =R.drawable.touristattractions; break;
case WLConstants.CATEGORY_VARIOUS_EVENTS: imageSource =R.drawable.all_cats; break;
case WLConstants.CATEGORY_ALL_FOOD_DRINK: imageSource =R.drawable.restaurants; break;
}
return imageSource;
}
#Override
public void onItemClick(AdapterView<?> adp, View view, int item, long arg3) {
try {
if("".equals(sc.getSearchLocation()) && (!(Util.locationFound(sc.getGeoLocation()) && sc.isUK()))){
UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Error", "Please provide location");
return;
}
ResultWrapper wrap = (ResultWrapper)view.getTag();
sc.setCategoryIds(wrap.catIds);
sc.setSubCategoryIds(wrap.subCatIds);
sc.setSearchType(wrap.searchType);
goSearch();
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
//UIFactory.displayAlert(new AlertDialog.Builder(this), "Error", e.getMessage());
}
}
private void applyListener(){
list.setOnItemClickListener(this);
changeLink.setOnClickListener(onclickListener);
}
private final void goSearch(){
try {
sc.setSearchString("");
Intent mainIntent = new Intent(HomeBrowse.this,SearchResults.class);
startActivity(mainIntent);
} catch (Throwable e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
}
}
public OnClickListener onclickListener = new OnClickListener(){
// #Override
public void onClick(View aView) {
try {
int componentId =aView.getId();
switch(componentId){
case R.id.tv_changeDateType:
Intent intent = new Intent(HomeBrowse.this,TimeLocationSettings.class);
startActivity(intent);
break;
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
Util.handleError(HomeBrowse.this, HomeBrowse.class, e.getMessage());
}
}
};
private void configComponents(){
String titleStr,location = "";
String dateCrtStr = Util.getDateCritieraStr(sc.getDateCriteria());
titleStr= dateCrtStr;
if(!"".equals(sc.getSearchLocation())){
location = sc.getSearchLocation();
}else if(sc.isNearMe()){
location="Near me";
}else{
location = "Postcode or town";
}
titleStr = titleStr + " - "+ location;
browseTitle.setText(titleStr);
changeLink.setTypeface(Typeface.DEFAULT, Typeface.BOLD);
}
public class BrowseRow {
private String name = "";
private String image = "";
private String categoryIds="0";
private String subCategoryIds="0";
private int searchType=1;
}
private Runnable returnRes = new Runnable() {
#Override
public void run() {
try {
browseTitle = (TextView)findViewById(R.id.browsePageTite);
changeLink = (TextView)findViewById(R.id.tv_changeDateType);
changeLink.setText(Html.fromHtml("<a href='#'>Change</a>"));
adapter=new BrowseAdapter();
applyListener();
configComponents();
fillResultRows(jsonArray, adapter);
if(jsonArray == null){
UIFactory.displayAlert(new AlertDialog.Builder(HomeBrowse.this), "Error", "Error Loading categories");
return;
}
list.setAdapter(adapter);
} catch (Throwable e) {
e.printStackTrace();
}
m_ProgressDialog.dismiss();
}
};
}

Not necessarily everything, because
The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in the hierarchy that has an id, and by saving the id of the currently focused view (all of which is restored by the default implementation of onRestoreInstanceState(Bundle)).
via http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
but if you override, you have to handle entire state by yourself.

You should be saving everything that describes the view's state that isn't saved elsewhere.
Save where the user is in a EditText, what value the EditText has, what dialog is currently displayed, etc. Your application should be able to reconstruct the exact state it was in from the bundle returned to in when it is paused by the user leaving the application and returning to it.

Related

JSONArray not updating immediately

Whenever in my Popular Movies Udacity project, I click on a movie poster in the favorites movie collection, which is maintained as a database offline, the URL list gets updated correctly but the JSONArray made in the AsyncTask does not update immediately.
package com.example.android.popularmovies;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.example.android.popularmovies.Database.Contract;
import com.example.android.popularmovies.utilities.NetworkUtils;
import com.facebook.stetho.Stetho;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private final static String BASE_POSTER_URL = "http://image.tmdb.org/t/p/w500/";
private static String OPTION = "OPTION";
TextView mNoFavorites;
// = new JSONArray();
JSONArray favoriteJsonArray;
int optionChosen;
private ProgressBar mProgessBar;
private TextView mErrorMessage;
private Button mRetry;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private URL url;
private ArrayList<String> posterList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
optionChosen = savedInstanceState.getInt(OPTION);
Stetho.initializeWithDefaults(this);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
mRecyclerView.setLayoutManager(mLayoutManager);
//mAdapter = new MainActivityAdapter(this, posterList, null);
//mRecyclerView.setAdapter(mAdapter);
mProgessBar = findViewById(R.id.progess_bar);
mErrorMessage = findViewById(R.id.error_message);
mRetry = findViewById(R.id.retry);
mNoFavorites = findViewById(R.id.no_favorites_yet);
favoriteJsonArray = new JSONArray();
tryToConnect(new View(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.preferences, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
try {
if (item.getItemId() == R.id.top_rated) {
mRecyclerView.setVisibility(View.VISIBLE);
mNoFavorites.setVisibility(View.INVISIBLE);
url = NetworkUtils.buildUrl("top_rated");
if (url != null) {
mErrorMessage.setVisibility(View.INVISIBLE);
mRetry.setVisibility(View.INVISIBLE);
new FetchMovies().execute(url, null, null);
}
optionChosen = 1;
}
if (item.getItemId() == R.id.most_popular) {
mRecyclerView.setVisibility(View.VISIBLE);
mNoFavorites.setVisibility(View.INVISIBLE);
url = NetworkUtils.buildUrl("most_popular");
if (url != null) {
mErrorMessage.setVisibility(View.INVISIBLE);
mRetry.setVisibility(View.INVISIBLE);
new FetchMovies().execute(url, null, null);
}
optionChosen = 2;
}
if (item.getItemId() == R.id.favorites) {
optionFavorites();
optionChosen = 3;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (java.lang.NullPointerException e) {
e.printStackTrace();
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
super.onResume();
if (optionChosen == 3)
optionFavorites();
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save the user's current game state
savedInstanceState.putInt(OPTION, optionChosen);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
/* public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
optionChosen = savedInstanceState.getInt(OPTION);
}
*/
private void optionFavorites() {
try {
String[] projection = {"movie_id", "favorite_poster_path"};
Cursor cursor = getContentResolver().query(
Contract.FavoriteMovieDatabase.CONTENT_URI,
projection,
null,
null,
null
);
ArrayList<String> posterPathArrayList = new ArrayList<>();
ArrayList<URL> urlArrayList = new ArrayList<>();
if (cursor != null && cursor.moveToFirst()) {
do {
String posterPath = cursor.getString(cursor.getColumnIndex(Contract.FavoriteMovieDatabase.FAVORITE_POSTER_PATH));
posterPathArrayList.add(BASE_POSTER_URL + posterPath);
int movieId = cursor.getInt(cursor.getColumnIndex("movie_id"));
URL favoriteURL = NetworkUtils.buildUrl("favorites", movieId);
urlArrayList.add(favoriteURL);
} while (cursor.moveToNext());
Log.d("urllist", "" + urlArrayList.toString());
new FetchFavorites().execute(urlArrayList, null, null);
mAdapter = new MainActivityAdapter(MainActivity.this, posterPathArrayList, favoriteJsonArray);
mRecyclerView.setAdapter(mAdapter);
} else {
mRecyclerView.setVisibility(View.INVISIBLE);
mNoFavorites.setVisibility(View.VISIBLE);
}
} catch (java.lang.NullPointerException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
private void noConnection() {
mErrorMessage.setText(R.string.no_connection);
mErrorMessage.setVisibility(View.VISIBLE);
mRetry.setText(R.string.retry);
mRetry.setVisibility(View.VISIBLE);
}
public void tryToConnect(View v) {
try {
url = NetworkUtils.buildUrl("most_popular");
} catch (MalformedURLException e) {
e.printStackTrace();
}
if (url != null) {
mErrorMessage.setVisibility(View.INVISIBLE);
mRetry.setVisibility(View.INVISIBLE);
new FetchMovies().execute(url, null, null);
}
}
private class FetchMovies extends AsyncTask<URL, Void, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgessBar.setVisibility(View.VISIBLE);
}
#Override
protected String doInBackground(URL... params) {
URL searchUrl = params[0];
String searchResults = null;
try {
searchResults = NetworkUtils.getResponseFromHttpUrl(searchUrl);
} catch (IOException e) {
e.printStackTrace();
}
return searchResults;
}
#Override
protected void onPostExecute(String searchResults) {
mProgessBar.setVisibility(View.INVISIBLE);
try {
posterList = new ArrayList<>();
if (searchResults != null && !searchResults.equals("")) {
JSONObject jsonObject = new JSONObject(searchResults);
JSONArray pageOne = jsonObject.getJSONArray("results");
int length = pageOne.length();
for (int i = 0; i < length; i++) {
JSONObject result = pageOne.getJSONObject(i);
String posterPath = BASE_POSTER_URL + result.getString("poster_path");
posterList.add(posterPath);
}
mAdapter = new MainActivityAdapter(MainActivity.this, posterList, pageOne);
mRecyclerView.setAdapter(mAdapter);
} else noConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
}
private class FetchFavorites extends AsyncTask<ArrayList<URL>, Void, ArrayList<String>> {
#Override
protected ArrayList<String> doInBackground(ArrayList<URL>... params) {
ArrayList<URL> urlArrayList = params[0];
ArrayList<String> stringArrayList = new ArrayList<>();
String searchResults;
URL searchUrl;
favoriteJsonArray = new JSONArray();
for (int i = 0; i < urlArrayList.size(); i++)
try {
searchUrl = urlArrayList.get(i);
searchResults = NetworkUtils.getResponseFromHttpUrl(searchUrl);
stringArrayList.add(searchResults);
} catch (IOException e) {
e.printStackTrace();
}
return stringArrayList;
}
#Override
protected void onPostExecute(ArrayList<String> stringArrayList) {
for (int i = 0; i < stringArrayList.size(); i++)
try {
favoriteJsonArray.put(new JSONObject(stringArrayList.get(i)));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
urlArrayList gets updated correctly when database changes occur, but favoriteJsonArray, which is updated inside AsyncTask, does not. Why?
There are two problems in you code:
in FetchFavorites.doInBackground(), you assign a new JSONArray() to favoriteJsonArray, but your mAdapter is still hold the old reference, which is an empty JSONArray.
you didn't call mAdapter.notifyDataSetChanged() after update the favoriteJsonArray.
You can solve the problems in to ways:
do not new JSONArray() in FetchFavorites.doInBackground(), and call mAdapter.notifyDataSetChanged() after update the favoriteJsonArray, but there is still a problem, you should clear the favoriteJsonArray every time you fetch the new data, and clear a JSONArray is kind of boring (there is no clear() method).
new JSONArray(), but edit your MainActivityAdapter, add a method like setFavorites(JSONArray array), and set the new created favoriteJsonArray to it then call mAdapter.notifyDataSetChanged().

Android get attribute value from xml

I am trying to make an app which uses last.fm's web API, sends a query for similar artists and returns all the names of the similar artists. It seems as though I manage to connect and get the xml response properly. However, I cannot extract the value of the name-attribute. I am using artistName = xmlData.getAttributeValue(null, "name"); but all it gives me is null.
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.*;
#SuppressWarnings("FieldCanBeLocal")
public class MainActivity extends Activity implements Observer {
private final String INPUTERROR = "Invalid/missing artist name.";
private NetworkCommunication nc;
private ArrayList<String> list;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nc = new NetworkCommunication();
nc.register(this);
list = new ArrayList<>();
ListView lv = (ListView)findViewById(R.id.ListView_similarArtistsList);
ArrayAdapter adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, list);
lv.setAdapter(adapter);
}
public void searchButton_Clicked(View v){
EditText inputField = (EditText)findViewById(R.id.editText_artistName);
String searchString = inputField.getText().toString();
searchString = cleanSearchString(searchString);
if(validateSearchString(searchString)){
nc.setSearchString(searchString);
nc.execute();
}
else{
Toast.makeText(MainActivity.this, INPUTERROR, Toast.LENGTH_SHORT).show();
}
}
private String cleanSearchString(String oldSearchString){
String newString = oldSearchString.trim();
newString = newString.replace(" ", "");
return newString;
}
private boolean validateSearchString(String searchString){
boolean rValue = true;
if(TextUtils.isEmpty(searchString)){
rValue = false;
}
return rValue;
}
#Override
public void update(String artistName) {
list.add(artistName);
}
}
Here is my Network Communications class:
import android.os.AsyncTask;
import android.util.Log;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
#SuppressWarnings("FieldCanBeLocal")
class NetworkCommunication extends AsyncTask<Object, String, Integer> implements Subject {
private final String MYAPIKEY = "--------------------------";
private final String ROOT = "http://ws.audioscrobbler.com/2.0/";
private final String METHOD = "?method=artist.getsimilar";
private ArrayList<Observer> observers;
private int amountOfArtists = 0;
private String foundArtistName;
private String searchString;
NetworkCommunication(){
observers = new ArrayList<>();
}
void setSearchString(String newSearchString){
searchString = newSearchString;
}
private XmlPullParser sendRequest(){
try{
URL url = new URL(ROOT + METHOD + "&artist=" + searchString + "&api_key=" + MYAPIKEY);
XmlPullParser receivedData = XmlPullParserFactory.newInstance().newPullParser();
receivedData.setInput(url.openStream(), null);
return receivedData;
}
catch (IOException | XmlPullParserException e){
Log.e("ERROR", e.getMessage(), e);
}
return null;
}
private int tryProcessData(XmlPullParser xmlData){
int artistsFound = 0;
String artistName;
int eventType;
try{
while ((eventType = xmlData.next()) != XmlPullParser.END_DOCUMENT) {
if (eventType == XmlPullParser.START_TAG) {
if(xmlData.getName().equals("name")){
artistName = xmlData.getAttributeValue(null, "name");
publishProgress(artistName);
artistsFound++;
}
}
}
}
catch (IOException | XmlPullParserException e){
Log.e("ERROR", e.getMessage(), e);
}
if (artistsFound == 0) {
publishProgress();
}
return artistsFound;
}
#Override
protected Integer doInBackground(Object... params) {
XmlPullParser data = sendRequest();
if(data != null){
return tryProcessData(data);
}
else{
return null;
}
}
#Override
protected void onProgressUpdate(String... values){
/*
if (values.length == 0) {
//No data found...
}
*/
if (values.length == 1) {
setFoundArtistName(values[0]);
notifyObserver();
}
super.onProgressUpdate(values);
}
private void setFoundArtistName(String newArtistName){
foundArtistName = newArtistName;
}
#Override
public void register(Observer newObserver) {
observers.add(newObserver);
}
#Override
public void unregister(Observer deleteObserver) {
observers.remove(deleteObserver);
}
#Override
public void notifyObserver() {
for (Observer o : observers) {
Log.i("my tag.... ", "name = " + foundArtistName);
o.update(foundArtistName);
}
}
}
Here's a screenshot of the xml response in Google Chrome:
The only thing I am interested in extracting at this moment is the the value of the Name-Element.
I am logging the value of foundArtistName (in the method notifyObserver) it gives me A LOT of "my tag.... name = null my tag.... name = null my tag.... name = null etc.."
EDIT: I tried using getText() instead of getAttributeValue(), but it also gives me null.
I found the solution. I was using: artistName = xmlData.getAttributeValue(null, "name");, when I really should've used: artistName = xmlData.nextText();

Telegram API RPC Timeout Exception

I am using telegram API to develop my application. Here is the whole code.
app_info.java:
package com.example.mytelegram;
import org.telegram.api.engine.AppInfo;
public class app_info extends AppInfo {
public app_info(int apiId, String deviceModel, String systemVersion,
String appVersion, String langCode) {
super(apiId, deviceModel, systemVersion, appVersion, langCode);
// TODO Auto-generated constructor stub
}
MemoryApiState.java:
package com.example.mytelegram;
import org.telegram.api.TLConfig;
import org.telegram.api.TLDcOption;
import org.telegram.api.engine.storage.AbsApiState;
import org.telegram.mtproto.state.AbsMTProtoState;
import org.telegram.mtproto.state.ConnectionInfo;
import org.telegram.mtproto.state.KnownSalt;
import java.util.ArrayList;
import java.util.HashMap;
public class MemoryApiState implements AbsApiState {
private HashMap<Integer, ConnectionInfo[]> connections = new HashMap<Integer, ConnectionInfo[]>();
private HashMap<Integer, byte[]> keys = new HashMap<Integer, byte[]>();
private HashMap<Integer, Boolean> isAuth = new HashMap<Integer, Boolean>();
private int primaryDc = 2; // I have tested application with primaryDc=0 to 10
public MemoryApiState(boolean isTest) {
connections.put(1, new ConnectionInfo[]{
new ConnectionInfo(1, 0, isTest ? "149.154.167.40" : "149.154.167.50", 443)
});
}
#Override
public synchronized int getPrimaryDc() {
return primaryDc;
}
#Override
public synchronized void setPrimaryDc(int dc) {
primaryDc = dc;
}
#Override
public synchronized boolean isAuthenticated(int dcId) {
if (isAuth.containsKey(dcId)) {
return isAuth.get(dcId);
}
return false;
}
#Override
public synchronized void setAuthenticated(int dcId, boolean auth) {
isAuth.put(dcId, auth);
}
#Override
public synchronized void updateSettings(TLConfig config) {
connections.clear();
HashMap<Integer, ArrayList<ConnectionInfo>> tConnections = new HashMap<Integer, ArrayList<ConnectionInfo>>();
int id = 0;
for (TLDcOption option : config.getDcOptions()) {
if (!tConnections.containsKey(option.getId())) {
tConnections.put(option.getId(), new ArrayList<ConnectionInfo>());
}
tConnections.get(option.getId()).add(new ConnectionInfo(id++, 0, option.getIpAddress(), option.getPort()));
}
for (Integer dc : tConnections.keySet()) {
connections.put(dc, tConnections.get(dc).toArray(new ConnectionInfo[0]));
}
}
#Override
public synchronized byte[] getAuthKey(int dcId) {
return keys.get(dcId);
}
#Override
public synchronized void putAuthKey(int dcId, byte[] key) {
keys.put(dcId, key);
}
#Override
public synchronized ConnectionInfo[] getAvailableConnections(int dcId) {
if (!connections.containsKey(dcId)) {
return new ConnectionInfo[0];
}
return connections.get(dcId);
}
#Override
public synchronized AbsMTProtoState getMtProtoState(final int dcId) {
return new AbsMTProtoState() {
private KnownSalt[] knownSalts = new KnownSalt[0];
#Override
public byte[] getAuthKey() {
return MemoryApiState.this.getAuthKey(dcId);
}
#Override
public ConnectionInfo[] getAvailableConnections() {
return MemoryApiState.this.getAvailableConnections(dcId);
}
#Override
public KnownSalt[] readKnownSalts() {
return knownSalts;
}
#Override
protected void writeKnownSalts(KnownSalt[] salts) {
knownSalts = salts;
}
};
}
#Override
public synchronized void resetAuth() {
isAuth.clear();
}
#Override
public synchronized void reset() {
isAuth.clear();
keys.clear();
}
}
MainActivity.java:
package com.example.mytelegram;
import java.io.IOException;
import org.telegram.api.TLAbsUpdates;
import org.telegram.api.TLConfig;
import org.telegram.api.TLNearestDc;
import org.telegram.api.auth.TLAuthorization;
import org.telegram.api.auth.TLSentCode;
import org.telegram.api.engine.ApiCallback;
import org.telegram.api.engine.AppInfo;
import org.telegram.api.engine.LoggerInterface;
import org.telegram.api.engine.RpcException;
import org.telegram.api.engine.TelegramApi;
import org.telegram.api.requests.TLRequestAuthSendCall;
import org.telegram.api.requests.TLRequestAuthSendCode;
import org.telegram.api.requests.TLRequestAuthSignIn;
import org.telegram.api.requests.TLRequestHelpGetConfig;
import org.telegram.api.requests.TLRequestHelpGetNearestDc;
import org.telegram.api.requests.TLRequestUpdatesGetState;
import org.telegram.api.updates.TLState;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends ActionBarActivity {
static Context context;
private static AppInfo ai;
private static void createApi() throws IOException {
String res = "No test";
boolean useTest = res.equals("test");
apiState = new MemoryApiState(useTest);
ai=new AppInfo(App_ID, "Sony", "???", "???", "en");
api = new TelegramApi(apiState, ai, new ApiCallback() {
#Override
public void onAuthCancelled(TelegramApi api) {
}
#Override
public void onUpdatesInvalidated(TelegramApi api) {
}
#Override
public void onUpdate(TLAbsUpdates updates) {
Log.d("tlg", "onUpdate(TLAbsUpdates updates)");
}
});
}
private static void login() throws IOException {
//TLNearestDc dcInfo = api.doRpcCallNonAuth(new TLRequestHelpGetNearestDc());
// api.switchToDc(dcInfo.getNearestDc());
//************ Exception raises here ************
TLConfig config = api.doRpcCallNonAuth(new TLRequestHelpGetConfig()); // I have An IOException Here (e=timeout Exception)
apiState.updateSettings(config);
Log.d("tlg","completed.");
String phone = "+9891********"; // I have tested app with the phone without plus too
Log.d("tlg","Sending sms to phone " + phone + "...");
TLSentCode sentCode = null;
try {
api.doRpcCallNonAuth(new TLRequestAuthSendCode(phone, 1, app_id, "__hash__", "en"));
} catch (RpcException e) {
if (e.getErrorCode() == 303) {
int destDC;
if (e.getErrorTag().startsWith("NETWORK_MIGRATE_")) {
destDC = Integer.parseInt(e.getErrorTag().substring("NETWORK_MIGRATE_".length()));
} else if (e.getErrorTag().startsWith("PHONE_MIGRATE_")) {
destDC = Integer.parseInt(e.getErrorTag().substring("PHONE_MIGRATE_".length()));
} else if (e.getErrorTag().startsWith("USER_MIGRATE_")) {
destDC = Integer.parseInt(e.getErrorTag().substring("USER_MIGRATE_".length()));
} else {
throw e;
}
api.switchToDc(destDC);
sentCode = api.doRpcCallNonAuth(new TLRequestAuthSendCode(phone, 0, app_id, "__hash__", "en"));
} else {
throw e;
}
}
Log.d("tlg","Activation code:");
String code = "123";
TLAuthorization auth = api.doRpcCallNonAuth(new TLRequestAuthSignIn(phone, sentCode.getPhoneCodeHash(), code));
apiState.setAuthenticated(apiState.getPrimaryDc(), true);
Log.d("tlg","Activation complete.");
TLState state = api.doRpcCall(new TLRequestUpdatesGetState());
Log.d("tlg","loaded.");
}
static MemoryApiState apiState;
static TelegramApi api;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=getApplicationContext();
try {
createApi();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
login();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//workLoop();
}
}
I have added telegram-api library (telegram-api-1.1.127-shadow.jar) in my eclipse 'libs' folder. The problem is Timeout Exception in MainActivity.java when I want to call RPC. Where is the problem? What should I do? My OS is windows 8.1 . I am searching for 2 whole days. I have read github codes too. They are very complicated for me. I can not figure out what should I do?
I have added INTERNET and ACCESS_NETWORK_STATE permisions to AndroidManifest.xml. I allways check the network connection when my application runs.
I found the problem in your code:
In MemoryApiState you should put, in the 'connections' map, 'primaryDc' as key and not '1'.

How to connect android app to node server?

I am trying out the WebSockets with Fallbacks transports for Android, Node.js and Atmosphere example. I get an the following error:
/home/mofa/NetBeansProjects/App/src/com/jullio/advisor/wAsyncChat.java:87: error: cannot access JsonParseException
return mapper.readValue(data, Message.class);
class file for org.codehaus.jackson.JsonParseException not found
/home/mofa/NetBeansProjects/App/src/com/jullio/advisor/wAsyncChat.java:68: error: cannot access ObjectCodec
return mapper.writeValueAsString(data);
class file for org.codehaus.jackson.ObjectCodec not found
Here is the androidchat code:
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.atmosphere.wasync.ClientFactory;
import org.atmosphere.wasync.Decoder;
import org.atmosphere.wasync.Encoder;
import org.atmosphere.wasync.Event;
import org.atmosphere.wasync.Function;
import org.atmosphere.wasync.Request;
import org.atmosphere.wasync.RequestBuilder;
import org.atmosphere.wasync.impl.AtmosphereClient;
import org.codehaus.jackson.map.ObjectMapper;
import java.io.IOException;
import java.util.Date;
public class wAsyncChat extends Activity {
private Button bt;
private TextView tv;
private String serverIpAddress = "http://10.0.2.2:8080";
private final static ObjectMapper mapper = new ObjectMapper();
private final Handler uiHandler = new Handler();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
setContentView(R.layout.main);
bt = (Button) findViewById(R.id.myButton);
tv = (TextView) findViewById(R.id.myTextView);
try {
AtmosphereClient client = ClientFactory.getDefault().newClient(AtmosphereClient.class);
RequestBuilder request = client.newRequestBuilder()
.method(Request.METHOD.GET)
.uri(serverIpAddress + "/chat")
.trackMessageLength(true)
.encoder(new Encoder<Message, String>() {
#Override
public String encode(Message data) {
try {
return mapper.writeValueAsString(data);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
})
.decoder(new Decoder<String, Message>() {
#Override
public Message decode(Event type, String data) {
data = data.trim();
// Padding
if (data.length() == 0) {
return null;
}
if (type.equals(Event.MESSAGE)) {
try {
return mapper.readValue(data, Message.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
} else {
return null;
}
}
})
.transport(Request.TRANSPORT.WEBSOCKET);
final org.atmosphere.wasync.Socket socket = client.create();
socket.on("message", new Function<Message>() {
#Override
public void on(final Message t) {
uiHandler.post(new Runnable() {
#Override
public void run() {
Date d = new Date(t.getTime());
tv.append("Author " + t.getAuthor() + "# " + d.getHours() + ":" + d.getMinutes() + ": " + t.getMessage() + "\n");
}
});
}
}).on(new Function<Throwable>() {
#Override
public void on(Throwable t) {
tv.setText("ERROR 3: " + t.getMessage());
t.printStackTrace();
}
}).open(request.build());
bt.setOnClickListener(new OnClickListener() {
String name = null;
public void onClick(View v) {
try {
EditText et = (EditText) findViewById(R.id.EditText01);
String str = et.getText().toString();
if (name == null) {
name = str;
}
socket.fire(new Message(name, str));
et.setText("");
Log.d("Client", "Client sent message");
} catch (Throwable e) {
tv.setText("ERROR 3: " + e.getMessage());
e.printStackTrace();
}
}
});
} catch (Throwable e) {
tv.setText("Unable to connect: " + e.getMessage());
e.printStackTrace();
}
}
}
I have the library for nodeserver connection. You can use it from git
SocketIO socketio = new SocketIO() {
#Override
public void onConnect() {
}
#Override
public void onDisconnect() {
}
#Override
public void onMessage(String message) {
Log.d("===Server Answer====",message);
}
};
socketio.Connect("192.168.0.1", 9000);
after onConnect() send the message:
socketio.send("Your message to socket");
it work with latest socketIO, and use RFC 6455 websocket protocol

Starting new activity through Runnable

I check if a string is NULL in a Thread, if its null, the Handler starts
a Runnable which starts a new Activity.
Everything works fine, but, after the new activity is displayed it switches back to the calling Activity and it crashes.
Here is a code snippet.
if(username==null)
{
dialogs.dismiss();
handlers.post(new MyRunable());
}
and Runnable is
public class MyRunable implements Runnable
{
public void run(){
Toast.makeText(ListActivity.this, "Your Connection is too slow",Toast.LENGTH_LONG).show();
startActivity(new Intent(ListActivity.this,Anim.class));
}
}
Here is my Full Source
package com.cram;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class BuddyListActivity extends Activity
{
String ss;
private static ProgressDialog dialogs,dialog;
private Thread downloadThreads;
boolean results=false;
Context ctx;
String[]ddd;
ListView lv1;
String [] icons;
BuddyDbAdapter adapter;
NetworkInfo netinfo;
Cursor cursor;
String values;
GetBuddy gb;
BuddyDB db;
String[] username;
String[] firstname;
String[] lastname;
String[] avatar;
String[] id;
File[] iconss;
Handler handlers;
boolean ready=false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buddy);
db=new BuddyDB(BuddyListActivity.this);
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/Fortheloveofhate.ttf");
TextView btxt = (TextView) findViewById(R.id.textbuddy);
btxt.setTypeface(font);
ctx=getApplicationContext();
lv1=(ListView)findViewById(R.id.list22);
netinfo=null;
adapter=new BuddyDbAdapter(BuddyListActivity.this);
netinfo=checkDataConnection(getApplicationContext());
handlers = new Handler();
adapter.open();
if(netinfo!=null)
{
downloadThreads = (Thread) getLastNonConfigurationInstance();
if (downloadThreads != null && downloadThreads.isAlive()) {
dialog = ProgressDialog.show(this, "Download", "downloading");
}
startdownload();
}
if(netinfo==null)
{
cursor=adapter.showBuddy();
if (cursor==null||cursor.moveToFirst()==false)
{
AlertDialog.Builder buddybox = new AlertDialog.Builder(this);
buddybox.setMessage("You have No Buddies Yet douche!!");
buddybox.setNeutralButton("Okay", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getBaseContext(),Anim.class));
}
});
buddybox.setCancelable(false);
buddybox.show();
}
else
{
cursor.moveToFirst();
int j=0;
ddd=new String[cursor.getCount()];
icons=new String [cursor.getCount()];
do
{
String firstName = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_FISTNAME));
String lastname = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_LASTNAME));
String Avatar = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.AVATAR));
ddd[j]=firstName+" "+lastname;
Log.d("Test", ddd[j]);
icons[j]=Avatar;
Log.d("Test", icons[j]);
j++;
}while(cursor.moveToNext());
iconss= new File[icons.length];
Log.d("Test",icons.length+"");
for (int q = 0; q < icons.length; q++) {
iconss[q] = new File(Download.ROOT +"/"+ icons[q]+".jpg");
Log.d("Test", iconss[q].getAbsolutePath().toString());
}
//adapter.close();
lv1.setAdapter(new BuddyAdapter(BuddyListActivity.this,R.layout.list1,ddd,iconss));
onDestroy();
}
}
}
private void box() {
// TODO Auto-generated method stub
cursor=adapter.showBuddy();
if (cursor==null||cursor.moveToFirst()==false)
{
dialogs.dismiss();
AlertDialog.Builder buddybox = new AlertDialog.Builder(this);
buddybox.setMessage("You have No Buddies Yet ass!!");
buddybox.setNeutralButton("Okay", new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(getBaseContext(),Anim.class));
}
});
buddybox.setCancelable(false);
buddybox.show();
}
}
private NetworkInfo checkDataConnection(Context applicationContext) {
final ConnectivityManager connMgr = (ConnectivityManager)BuddyListActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo=null;
final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if(wifi.isConnected()||mobile.isConnected())
{networkinfo=connMgr.getActiveNetworkInfo();
return networkinfo;
}
else
{
return null;
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if (adapter != null) {
adapter.close();
}
}
#Override
protected void onStop()
{
super.onStop();
finish();
}
private void startdownload() {
dialogs = ProgressDialog.show(BuddyListActivity.this, "Contacting Our Servers", "Geting Your Buddies Avatar");
downloadThreads = new MyThread();
downloadThreads.start();
}
public class MyThread extends Thread {
#Override
public void run() {
try {
new Thread();
GetBuddy tt=new GetBuddy();
String xml=tt.get();
if(xml==null)
{ dialogs.dismiss();
handlers.post(new MyRunable());
ready=false;
//downloadThreads.suspend();
}
final Download cd = new Download();
results = cd.downloadBitmap();
if(results)
{
BuddyParse bp=new BuddyParse();
try {
username=bp.show(xml);
// if(username==null)
// { dialogs.dismiss();
// handlers.post(new MyRunable());
// //downloadThreads.suspend();
// }
firstname=bp.firstname();
lastname=bp.lastname();
avatar=bp.avatar();
id=bp.id();
adapter.deleteAll();
ready=true;
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
catch (SAXException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
// Log.d("len", username.length+"");
for(int k=0; k<username.length;k++)
{
adapter.insertBuddy(id[k], username[k], firstname[k], lastname[k], avatar[k]);
Log.d("Test", id[k]+username[k]+firstname[k]+lastname[k]+avatar[k]+"");
}
box();
cursor=adapter.showBuddy();
cursor.moveToFirst();
int i=0;
ddd=new String[cursor.getCount()];
icons=new String [cursor.getCount()];
do
{
String firstName = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_FISTNAME));
String lastname = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.BUDDY_LASTNAME));
String Avatar = cursor.getString(cursor.getColumnIndex(BuddyDbAdapter.AVATAR));
ddd[i]=firstName+" "+lastname;
Log.d("Test", ddd[i]);
icons[i]=Avatar;
Log.d("Test",icons[i]);
i++;
}while(cursor.moveToNext());
iconss= new File[avatar.length];
for (int k = 0; k < avatar.length; k++) {
iconss[k] = new File(Download.ROOT+"/"+avatar[k]+".jpg");
Log.d("Test", iconss[k].getAbsolutePath()+"thread");
//Log.d("Test", ddd[k]);
}
if (results&&ready)
{
dialogs.dismiss();
handlers.post(new MyRuns());
}
}
// else
// { dialogs.dismiss();
// handlers.post(new MyRunable());
//
// }
}finally {
}
}
}
public class MyRuns implements Runnable {
public void run() {
ready=true;
lv1.setAdapter(new BuddyAdapter(ctx,R.layout.list1,ddd,iconss));
onDestroy();
}
}
public class MyRunable implements Runnable {
public void run() {
//Toast.makeText(BuddyListActivity.this, "Your Connection is too slow", Toast.LENGTH_LONG).show();
startActivity(new Intent(BuddyListActivity.this,Anim.class));
}
}
}
use main thread to start your thread check runOnUiThread(Runnable action)
Setting RETURN in the try catch block and setting
android:noHistory="true" in Android Manifest for all activities fixed my problem
If you are trying to create an async task in a different thread I would recommend you to use the following class:
private final class MyRunnable extends
AsyncTask<Void, Void, Document> {
protected Document doInBackground(Void... params) {
}
protected void onPostExecute(Document result) {
}
}
You only have to change the parameters.
More info here:
http://developer.android.com/reference/android/os/AsyncTask.html
Finish your current activity when starting another activity from runnable .

Categories

Resources