I Am trying to include in app purchase and i have successfully through in showing up SKUs available.Now I want to make a fake purchase.So I used appId = "android.test.purchased". For the first time it worked flawlessly, but from next it is throwing exception as below.
Attempt to invoke virtual method 'android.content.IntentSender android.app.PendingIntent.getIntentSender()' on a null object reference
Has anybody come across such situation?
package com.inappbilling.poc;
import java.util.ArrayList;
import org.json.JSONObject;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import com.android.vending.billing.IInAppBillingService;
public class TestInAppPurchase extends Activity {
private final static String SERVICE_INTENT = "com.android.vending.billing.InAppBillingService.BIND";
private static final String _TAG = "BILLING ACTIVITY";
private final String _testSku = "android.test.purchased";
//available skus
static final String SKU_7DAYS = "7days";
static final String SKU_30DAYS = "30days";
private Button _7daysPurchase = null;
private Button _30daysPurchase = null;
private IInAppBillingService _service = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(_TAG, "created");
_7daysPurchase = ( Button ) findViewById(R.id.sevendays_btn);
_30daysPurchase = ( Button ) findViewById(R.id.thirtydays_btn);
_7daysPurchase.setOnClickListener(_purchaseListener);
_30daysPurchase.setOnClickListener(_purchaseListener);
}
#Override
protected void onStart() {
super.onStart();
}
OnClickListener _purchaseListener = new OnClickListener() {
#Override
public void onClick(View v) {
switch ( v.getId() ) {
case R.id.sevendays_btn:
doPurchase();
break;
case R.id.thirtydays_btn:
break;
}
}
};
private ServiceConnection _serviceConnection = new ServiceConnection() {
#Override
public void onServiceConnected(ComponentName name, IBinder service) {
_service = IInAppBillingService.Stub.asInterface( service );
Log.d(_TAG, _service.toString());
}
#Override
public void onServiceDisconnected(ComponentName name) {
_service = null;
Log.d(_TAG, "destroyed");
}
};
private void doPurchase(){
if ( _service == null) {
Log.e( _TAG , "Billing failed: billing service is null ");
return;
}
ArrayList testSku = new ArrayList( );
testSku.add( _testSku );
Bundle querySkus = new Bundle();
querySkus.putStringArrayList("ITEM_ID_LIST", testSku);
Bundle skuDetails ;
try {
skuDetails = _service.getSkuDetails(3, getPackageName(), "inapp", querySkus);
int response = skuDetails.getInt("RESPONSE_CODE");
if( response == 0 ){
ArrayList<String> responseList = new ArrayList<String>( );
responseList = skuDetails.getStringArrayList("DETAILS_LIST");
for( String responseString : responseList ) {
JSONObject jobj = new JSONObject( responseString );
String sku = jobj.getString("productId");
if( sku.equals( _testSku )){
Bundle buyIntentBundle = _service.getBuyIntent(3, getPackageName(), sku ,"inapp","" );
PendingIntent pendingIntent = buyIntentBundle.getParcelable("BUY_INTENT");
startIntentSenderForResult(pendingIntent.getIntentSender(),
1001, new Intent(), Integer.valueOf(0), Integer.valueOf(0),
Integer.valueOf(0));
}
}
} else {
Log.e( _TAG , "Failed " );
}
} catch (Exception e) {
Log.e( _TAG, "Caught exception !"+ e.getMessage() );
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == 1001 ){
String purchaseData = data.getStringExtra("INAPP_PURCHASE_DATA");
if( resultCode == RESULT_OK ){
try{
JSONObject jobj = new JSONObject( purchaseData );
String sku = jobj.getString(_testSku);
String paid = jobj.getString("price");
Log.v("SKU DATA", sku +"============"+ paid);
}catch( Exception e) {
e.printStackTrace();
}
}
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if( _serviceConnection != null ){
unbindService( _serviceConnection );
}
}
}
Hi get to know about the solution for the prob mentioned above.To get rid of this you should clear the cache of Google play app on your android phone.When we make any in app purchase are details are maintained by the Google play services.So once the a product is purchased play Isolates the particular sku,to avoid duplicate purchase.
Note - This is actually for testing purpose. Because Google play services maintains the record of all applications which have in app purchase or Oauth or any other facilities.
Hope it would help some one who is facing this problem.
When we try to purchase the already owned item we will get the null exception at Pending Intent.
So you need to check whether the product is purchased or not and then initiate the purchase only if the product is not purchased.
Product purchase state can be retrieved using the function
boolean haspurchase = inventory.hasPurchase(sku_id);
It worked for me..!!
Related
I'm not being able to publish messages with Nearby.Messages, although the Subscribe method seems to be working well. I've already created new credentials for Nearby in my Google Developer Console, made sure both the SHA-1's of my computer, and the app in the PlayStore are associated with those credentials, as well as the app's package name.
As I added an OnSuccessListener to both Publish, and Subscribe, within onStart, I noticed that the Subscribe method works well, while the Publish one Fails. The error code is 2806 - Forbidden.
It used to work just the way it is. Only after I uploaded the app to the Play Store, and had to add a new OAuth 2.0 Client ID, did it stop working, on publishing the desired Message. I've maintained the previous ID, as well, so I continue testing on my device, installing from Android Studio. Meanwhile, I've also re-created both ID's in the Console, but the problem still occurs. The Nearby function is also active, on the Console.
Adding the Fragment's code below:
package co.thanker.fragments;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import com.bumptech.glide.Glide;
import com.google.android.gms.nearby.Nearby;
import com.google.android.gms.nearby.messages.Message;
import com.google.android.gms.nearby.messages.MessageListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.auth.FirebaseAuth;
import co.thanker.R;
public class FindFragment extends Fragment {
private final String TAG = "FindFragment";
private final String OUR_USER_ID = "our-user-id";
private final String OUR_USER_COUNTRY = "our-user-country";
private final String USER_ID_STRING = "user-id-string";
private final String THANKER_ID_STRING = "thanker-id-string";
private final String USER_COUNTRY = "user-country";
private final String CONTINUE_SENDING_ID = "continue-sending-user-id";
private final String ACTIVATED_THANKS = "activated-thanks";
private Message mSendingMessage;
private MessageListener mMessageListener;
private FirebaseAuth mAuth;
private String mCountry;
private String mUserId;
private boolean mActivatedThanks;
private ImageView mGifFind;
private ProgressBar mProgressBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_find, container, false);
Log.v(TAG, "Entering FindFragment");
mAuth = FirebaseAuth.getInstance();
mActivatedThanks = true;
mGifFind = (ImageView) view.findViewById(R.id.gif_find);
mProgressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
if(getArguments() != null){
Log.v(TAG, "Finding passing Country. Thanks Fragment. GetArguments() exists");
mUserId = getArguments().getString(THANKER_ID_STRING);
mCountry = getArguments().getString(OUR_USER_COUNTRY);
}
else {
Log.v(TAG, "Finding passing Country. Thanks Fragment. GetArguments() does not exist");
mUserId = mAuth.getCurrentUser().getUid();
}
Log.v(TAG, "Nearby. Thanks got in Thanks Fragment: " + mCountry + ". User ID: " + mUserId);
if(getActivity() != null){
Glide.with(getActivity()).load(R.drawable.nearby_search).into(mGifFind);
mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getActivity(), R.color.colorPrimaryDark), PorterDuff.Mode.SRC_IN );
}
mMessageListener = new MessageListener() {
#Override
public void onFound(Message message) {
Log.d(TAG, "Nearby. Found message: " + new String(message.getContent()));
final String otherUserId = new String(message.getContent()).trim();
if(!otherUserId.equalsIgnoreCase(mAuth.getCurrentUser().getUid())){
Fragment otherUserProfileFragment = new OtherProfileFragment();
Bundle userInfoBundle = new Bundle();
userInfoBundle.putString(USER_ID_STRING, otherUserId);
userInfoBundle.putString(OUR_USER_ID, mUserId);
userInfoBundle.putString(OUR_USER_COUNTRY, mCountry);
userInfoBundle.putBoolean(ACTIVATED_THANKS, mActivatedThanks);
userInfoBundle.putBoolean(CONTINUE_SENDING_ID, true);
otherUserProfileFragment.setArguments(userInfoBundle);
if(getActivity() != null){
mProgressBar.setVisibility(View.GONE);
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, otherUserProfileFragment).addToBackStack(null).commit();
}
}
}
#Override
public void onLost(Message message) {
Log.d(TAG, "Nearby. Lost sight of message: " + new String(message.getContent()));
}
};
byte [] userIdInBytes = mUserId.getBytes();
mSendingMessage = new Message(userIdInBytes);
Log.v(TAG, "Nearby. Sending Message: " + new String(mSendingMessage.getContent()));
return view;
}
#Override
public void onStart(){
super.onStart();
if(getActivity() != null){
Nearby.getMessagesClient(getActivity()).publish(mSendingMessage).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.v(TAG, "Nearby. Publishing key");
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.v(TAG, "Nearby. Couldn\'t publish key. Error: " + e.toString());
}
});
Nearby.getMessagesClient(getActivity()).subscribe(mMessageListener).addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
Log.v(TAG, "Nearby. Subscribing incoming message");
}
});
}
}
#Override
public void onStop(){
if(getActivity() != null){
Nearby.getMessagesClient(getActivity()).unpublish(mSendingMessage);
Nearby.getMessagesClient(getActivity()).unsubscribe(mMessageListener);
Log.v(TAG, "Nearby. Unpublished keys");
}
super.onStop();
}
}
Really appreciate your help!
Best regards,
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();
Hi we are trying to get Google Play subscriptions information connected to one of our Android apps. The first step is authorize and get subscriptions json
Has anyone succeed to work with Google Play Developer API? Authorization? Subscriptions list?
Below is our experience
When I use authScope = "https://www.googleapis.com/auth/androidpublisher";
(took it from Google Example)
private static final String authScope = "https://www.googleapis.com/auth/androidpublisher";
am.getAuthToken(
myAccount, // Account retrieved using getAccountsByType()
authScope, // Auth scope
options, // Authenticator-specific options
this, // Your activity
new OnTokenAcquired(), // Callback called when a token is successfully acquired
new Handler(new OnError())); // Callback called if an error occurs
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
#Override
public void run(AccountManagerFuture<Bundle> accountManagerFuture) {
addProcessMessage("OnTokenAcquired");
// Get the result of the operation from the AccountManagerFuture.
try {
Bundle bundle = accountManagerFuture.getResult();
// The token is a named value in the bundle. The name of the value
// is stored in the constant AccountManager.KEY_AUTHTOKEN.
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
addProcessMessage("OnTokenAcquired token=" + token);
processAcessSubscriptions(token);
} catch ( Exception ae ) {
Log.e(TAG, "OnTokenAcquired Exception=" + ae.getMessage(), ae);
setProcessFailed(ae.getMessage());
}
}
}
I can’t get token, because I get exception below
OnTokenAcquired Exception=Unknown
android.accounts.AuthenticatorException: Unknown
at android.accounts.AccountManager.convertErrorToException(AccountManager.java:2213)
at android.accounts.AccountManager.-wrap0(AccountManager.java)
at android.accounts.AccountManager$AmsTask$Response.onError(AccountManager.java:2056)
at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
at android.os.Binder.execTransact(Binder.java:565)
When I use authScope = "Manage your tasks";
(took it from another Google Example)
private static final String authScope = "Manage your tasks";
I'm receiving token, but on ProductPurchase product = purchases.products().get(PACKAGE_NAME, TEST_SKU, token).execute();
I am getting getProducts Exception=400 Bad Request
See below
HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
AssetManager assetManager = getAssets();
InputStream is = assetManager.open(CLIENT_SECRET_JSON);
GoogleCredential credential = GoogleCredential.fromStream(is).createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).setApplicationName(APP_NAME).build();
AndroidPublisher.Purchases purchases = publisher.purchases();
ProductPurchase product = purchases.products().get(PACKAGE_NAME, TEST_SKU, token).execute();
getProducts Exception=400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Value",
"reason" : "invalid"
} ],
"message" : "Invalid Value"
}
com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Value",
"reason" : "invalid"
} ],
"message" : "Invalid Value"
}
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:321)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1065)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
Main Activity
package com.lemi.affilatemanager;
import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AccountManagerCallback;
import android.accounts.AccountManagerFuture;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.androidpublisher.AndroidPublisher;
import com.google.api.services.androidpublisher.AndroidPublisherScopes;
import com.google.api.services.androidpublisher.model.ProductPurchase;
import java.io.InputStream;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int DIALOG_ACCOUNTS = 11;
public static final String CLIENT_SECRET_JSON = "service_account.json";//"client_secret.json";
public static final String APP_NAME = "AffilateManager";
public static final String PACKAGE_NAME = "com.lemi.keywordmarketingautoresponder";
private AccountManager accountManager;
private Account myAccount;
protected static final int PERMISSION_REQUEST = 10;
private TextView resultText;
private StringBuilder testMessage = new StringBuilder();
private ProgressBar progressBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
accountManager = AccountManager.get(this);
setContentView(R.layout.activity_main);
resultText = (TextView) findViewById(R.id.test_result);
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
Button startProcessButton = (Button) findViewById(R.id.start_process_btn);
startProcessButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
test();
}
});
}
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_ACCOUNTS:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select a Google account");
final Account[] accounts = accountManager.getAccountsByType("com.google");
final int size = accounts.length;
String[] names = new String[size];
for (int i = 0; i < size; i++) {
names[i] = accounts[i].name;
}
builder.setItems(names, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Stuff to do when the account is selected by the user
myAccount = accounts[which];
processGetAuthToken();
}
});
return builder.create();
}
return null;
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
Log.i(TAG, "onRequestPermissionsResult requestCode=" + requestCode);
switch (requestCode) {
case PERMISSION_REQUEST:
test();
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
private void test () {
boolean canReadContacts = ContextCompat.checkSelfPermission(this, Manifest.permission.GET_ACCOUNTS) == PackageManager.PERMISSION_GRANTED;
Log.i(TAG, "test canReadContacts=" + canReadContacts);
if ( !canReadContacts ) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.GET_ACCOUNTS}, PERMISSION_REQUEST);
return;
}
startProcess();
if ( getAccount() ) {
processGetAuthToken();
}
}
private void startProcess () {
progressBar.setVisibility(View.VISIBLE);
testMessage.delete(0, testMessage.length());
resultText.setText(testMessage);
}
private void setProcessFailed ( final String message ) {
runOnUiThread(new Runnable() {
#Override
public void run() {
progressBar.setVisibility(View.INVISIBLE);
addProcessMessage(message);
}});
}
private void addProcessMessage ( String message ) {
Log.i(TAG, message);
testMessage.append("\n").append(message);
resultText.setText(testMessage);
}
private boolean getAccount () {
Account[] accounts = accountManager.getAccountsByType("com.google");
if ( accounts == null || accounts.length < 1 || accounts.length > 1) {
showDialog(DIALOG_ACCOUNTS);
return false;
}
myAccount = accounts[0];
return true;
}
private static final String authScope = "https://www.googleapis.com/auth/androidpublisher";
//private static final String authScope = "Manage your tasks";//https://www.googleapis.com/auth/androidpublisher";
//private static final String authScope = "Financial Access Enabled Account";
private void processGetAuthToken() {
addProcessMessage("Start processGetAuthToken process...");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
AccountManager am = AccountManager.get(this);
Bundle options = new Bundle();
//am.invalidateAuthToken(accountType, authTocken);
am.getAuthToken(
myAccount, // Account retrieved using getAccountsByType()
authScope, // Auth scope
options, // Authenticator-specific options
this, // Your activity
new OnTokenAcquired(), // Callback called when a token is successfully acquired
new Handler(new OnError())); // Callback called if an error occurs
}
private static final String TEST_SKU = "subs:com.lemi.keywordmarketingautoresponder:keywordmarketingautoresponder.1month.1keyword";
private void processAcessSubscriptions( String token ) {
addProcessMessage("processAcessSubscriptions token=" + token);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//AndroidPublisher.Purchases ap = null;
try {
//NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
HttpTransport httpTransport = AndroidHttp.newCompatibleTransport();
JacksonFactory jsonFactory = JacksonFactory.getDefaultInstance();
AssetManager assetManager = getAssets();
InputStream is = assetManager.open(CLIENT_SECRET_JSON);
GoogleCredential credential = GoogleCredential.fromStream(is).createScoped(Collections.singleton(AndroidPublisherScopes.ANDROIDPUBLISHER));
AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).setApplicationName(APP_NAME).build();
addProcessMessage("processAcessSubscriptions publisher=" + publisher);
getProducts(token, publisher);
} catch (Exception e) {
Log.e(TAG, "processAcessSubscriptions Exception:" + e.getMessage(), e);
setProcessFailed(e.getMessage());
}
}
private void getProducts(final String token, final AndroidPublisher publisher) {
new Thread(new Runnable() {
public void run() {
try {
AndroidPublisher.Purchases purchases = publisher.purchases();
addProcessMessage("getProducts purchases=" + purchases);
ProductPurchase product = purchases.products().get(PACKAGE_NAME, TEST_SKU, token).execute();
Integer purchaseState = product.getPurchaseState();
addProcessMessage("getProducts purchaseState=" + purchaseState);
long time = product.getPurchaseTimeMillis();
int consuptionState = product.getConsumptionState();
String developerPayload = product.getDeveloperPayload();
addProcessMessage("getProducts time=" + time + " consuptionState=" + consuptionState + " developerPayload=" + developerPayload);
} catch ( Exception e ) {
Log.e(TAG, "getProducts Exception=" + e.getMessage(), e);
setProcessFailed(e.getMessage());
}
}
}).start();
}
private class OnTokenAcquired implements AccountManagerCallback<Bundle> {
#Override
public void run(AccountManagerFuture<Bundle> accountManagerFuture) {
addProcessMessage("OnTokenAcquired");
// Get the result of the operation from the AccountManagerFuture.
try {
Bundle bundle = accountManagerFuture.getResult();
// The token is a named value in the bundle. The name of the value
// is stored in the constant AccountManager.KEY_AUTHTOKEN.
String token = bundle.getString(AccountManager.KEY_AUTHTOKEN);
addProcessMessage("OnTokenAcquired token=" + token);
processAcessSubscriptions(token);
} catch ( Exception ae ) {
Log.e(TAG, "OnTokenAcquired Exception=" + ae.getMessage(), ae);
setProcessFailed(ae.getMessage());
}
}
}
private class OnError implements Handler.Callback {
#Override
public boolean handleMessage(Message message) {
Log.i(TAG, "OnError handleMessage=" + message);
setProcessFailed(message.toString());
return false;
}
}
}
I am building an android app in which I am calling api.ai. I want to parse the response and display it to users. Earlier I had written code in node.js which is as below:
function sendMessageToApiAi(options,botcontext) {
var message = options.message; // Mandatory
var sessionId = options.sessionId || ""; // optinal
var callback = options.callback;
if (!(callback && typeof callback == 'function')) {
return botcontext.sendResponse("ERROR : type of options.callback should be function and its Mandatory");
}
var nlpToken = options.nlpToken;
if (!nlpToken) {
if (!botcontext.simpledb.botleveldata.config || !botcontext.simpledb.botleveldata.config.nlpToken) {
return botcontext.sendResponse("ERROR : token not set. Please set Api.ai Token to options.nlpToken or context.simpledb.botleveldata.config.nlpToken");
} else {
nlpToken = botcontext.simpledb.botleveldata.config.nlpToken;
}
}
var query = '?v=20150910&query='+ encodeURIComponent(message) +'&sessionId='+context.simpledb.roomleveldata.c1+'&timezone=Asia/Calcutta&lang=en '
var apiurl = "https://api.api.ai/api/query"+query;
var headers = { "Authorization": "Bearer " + nlpToken};
botcontext.simplehttp.makeGet(apiurl, headers, function(context, event) {
if (event.getresp) {
callback(event.getresp);
} else {
callback({})
}
});
}
My Android code is as below:
package com.example.pramod.apidev;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
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 java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity{
private Button listenButton;
private TextView resultTextView;
private EditText inputText;
private static String API_URL = "https://api.api.ai/api/query";
private static String API_KEY = "d05b02dfe52f4b5f969ba1257cffac37";
private static String query;
private static String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listenButton = (Button) findViewById(R.id.listenButton);
resultTextView = (TextView) findViewById(R.id.resultTextView);
inputText = (EditText)findViewById(R.id.inputText);
s = inputText.getText().toString();
query = "?v=20150910&query=hi" +"&sessionId=1480181847573api&timezone=Asia/Calcutta&lang=en";
listenButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
try {
URL url = new URL(API_URL + query + "&apiKey=" + API_KEY);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
}
}
});
}
}
I need help in following:
(i) How can call the Api.ai since I am getting an error 401? Can some one tell how to exactly call Api.ai using the Node.js code?
(ii) How can parse the response and display it to users?
Thanks in advance
I'm connecting with api.ai from android app.
The steps to do are the following
1. Add the dependencies:
compile 'ai.api:libai:1.2.2'
compile 'ai.api:sdk:2.0.1#aar'
2. Create an activity implementing AIListener.
3. Declare AIService,and AIDataService:
private AIService aiService;
private AIDataService aiDataService;
4. Initialize config, services and add listener:
final ai.api.android.AIConfiguration config = new ai.api.android.AIConfiguration("API_KEY",
ai.api.android.AIConfiguration.SupportedLanguages.Spanish,
ai.api.android.AIConfiguration.RecognitionEngine.System);
// Use with text search
aiDataService = new AIDataService(this, config);
// Use with Voice input
aiService = AIService.getService(this, config);
aiService.setListener(this);
5. Execute async task to make request:
AIRequest aiRequest = new AIRequest();
aiRequest.setQuery(request);
//request--any string you want to send to chat bot to get there corresponding response.
if(aiRequest==null) {
throw new IllegalArgumentException("aiRequest must be not null");
}
final AsyncTask<AIRequest, Integer, AIResponse> task =
new AsyncTask<AIRequest, Integer, AIResponse>() {
private AIError aiError;
#Override
protected AIResponse doInBackground(final AIRequest... params) {
final AIRequest request = params[0];
try {
final AIResponse response = aiDataService.request(request);
// Return response
return response;
} catch (final AIServiceException e) {
aiError = new AIError(e);
return null;
}
}
#Override
protected void onPostExecute(final AIResponse response) {
if (response != null) {
onResult(response);
} else {
onError(aiError);
}
}
};
task.execute(aiRequest);
6. onResult method
Result result = response.getResult();
If the result is a string, the string with the response will be in:
String speech = result.getFulfillment().getSpeech();
Regards.
Nuria
You can refer the official documentation of integrating API.AI here -
https://github.com/api-ai/apiai-android-client
Happy coding.
I'm trying to get all the user chats (created in my database) using an ArrayList and Recyclerview.Adapter but only first item from my ArrayList is being shown on my emulator screen.
Here's the corresponding code:
MainActivity:
package com.wipro.chat.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import com.wipro.chat.R;
import com.wipro.chat.adapter.ChatRoomsAdapter;
import com.wipro.chat.app.Config;
import com.wipro.chat.app.EndPoints;
import com.wipro.chat.app.MyApplication;
import com.wipro.chat.gcm.GcmIntentService;
import com.wipro.chat.gcm.NotificationUtils;
import com.wipro.chat.helper.SimpleDividerItemDecoration;
import com.wipro.chat.model.ChatRoom;
import com.wipro.chat.model.Message;
public class MainActivity extends AppCompatActivity {
private String TAG = MainActivity.class.getSimpleName();
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private BroadcastReceiver mRegistrationBroadcastReceiver;
private ArrayList<ChatRoom> chatRoomArrayList;
private ChatRoomsAdapter mAdapter;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/**
* Check for login session. If not logged in launch
* login activity
* */
if (MyApplication.getInstance().getPrefManager().getUser() == null) {
launchLoginActivity();
}
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
/**
* Broadcast receiver calls in two scenarios
* 1. gcm registration is completed
* 2. when new push notification is received
* */
mRegistrationBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
// checking for type intent filter
if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
// gcm successfully registered
// now subscribe to `global` topic to receive app wide notifications
subscribeToGlobalTopic();
} else if (intent.getAction().equals(Config.SENT_TOKEN_TO_SERVER)) {
// gcm registration id is stored in our server's MySQL
Log.e(TAG, "GCM registration id is sent to our server");
} else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
// new push notification is received
handlePushNotification(intent);
}
}
};
chatRoomArrayList = new ArrayList<>();
mAdapter = new ChatRoomsAdapter(this, chatRoomArrayList);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.addItemDecoration(new SimpleDividerItemDecoration(
getApplicationContext()
));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(mAdapter);
recyclerView.addOnItemTouchListener(new ChatRoomsAdapter.RecyclerTouchListener(getApplicationContext(), recyclerView, new ChatRoomsAdapter.ClickListener() {
#Override
public void onClick(View view, int position) {
// when chat is clicked, launch full chat thread activity
ChatRoom userChatRoom = chatRoomArrayList.get(position);
Intent intent = new Intent(MainActivity.this, ChatRoomActivity.class);
intent.putExtra("user_id", userChatRoom.getId());
intent.putExtra("name", userChatRoom.getName());
startActivity(intent);
}
#Override
public void onLongClick(View view, int position) {
}
}));
/**
* Always check for google play services availability before
* proceeding further with GCM
* */
if (checkPlayServices()) {
registerGCM();
fetchChatRooms();
}
}
/**
* Handles new push notification
*/
private void handlePushNotification(Intent intent) {
/*int type = intent.getIntExtra("type", -1);
// if the push is of chat room message
// simply update the UI unread messages count
if (type == Config.PUSH_TYPE_CHATROOM) {
Message message = (Message) intent.getSerializableExtra("message");
String chatRoomId = intent.getStringExtra("chat_room_id");
if (message != null && chatRoomId != null) {
updateRow(chatRoomId, message);
}
} else if (type == Config.PUSH_TYPE_USER) {
// push belongs to user alone
// just showing the message in a toast
Message message = (Message) intent.getSerializableExtra("message");
Toast.makeText(getApplicationContext(), "New push: " + message.getMessage(), Toast.LENGTH_LONG).show();
}*/
Message message = (Message) intent.getSerializableExtra("message");
String userChatRoomId = intent.getStringExtra("user_id");
if (message != null && userChatRoomId != null) {
updateRow(userChatRoomId, message);
}
}
/**
* Updates the chat list unread count and the last message
*/
private void updateRow(String chatRoomId, Message message) {
for (ChatRoom cr : chatRoomArrayList) {
if (cr.getId().equals(chatRoomId)) {
int index = chatRoomArrayList.indexOf(cr);
cr.setLastMessage(message.getMessage());
cr.setUnreadCount(cr.getUnreadCount() + 1);
chatRoomArrayList.remove(index);
chatRoomArrayList.add(index, cr);
break;
}
}
mAdapter.notifyDataSetChanged();
}
/**
* fetching the chat rooms by making http call
*/
private void fetchChatRooms() {
StringRequest strReq = new StringRequest(Request.Method.GET,
EndPoints.CHAT_ROOMS, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e(TAG, "response: " + response);
try {
JSONObject obj = new JSONObject(response);
// check for error flag
if (obj.getBoolean("error") == false) {
JSONArray chatRoomsArray = obj.getJSONArray("chat_rooms");
for (int i = 0; i < chatRoomsArray.length(); i++) {
JSONObject chatRoomsObj = (JSONObject) chatRoomsArray.get(i);
ChatRoom cr = new ChatRoom();
cr.setId(chatRoomsObj.getString("user_id"));
cr.setName(chatRoomsObj.getString("name"));
cr.setLastMessage("");
cr.setUnreadCount(0);
cr.setTimestamp(chatRoomsObj.getString("created_at"));
chatRoomArrayList.add(cr);
}
} else {
// error in fetching chat rooms
Toast.makeText(getApplicationContext(), "" + obj.getJSONObject("error").getString("message"), Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
Log.e(TAG, "json parsing error: " + e.getMessage());
Toast.makeText(getApplicationContext(), "Json parse error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
mAdapter.notifyDataSetChanged();
// subscribing to all chat room topics
//subscribeToAllTopics();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
NetworkResponse networkResponse = error.networkResponse;
Log.e(TAG, "Volley error: " + error.getMessage() + ", code: " + networkResponse);
Toast.makeText(getApplicationContext(), "Volley error: " + error.getMessage(), Toast.LENGTH_SHORT).show();
}
});
//Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq);
}
// subscribing to global topic
private void subscribeToGlobalTopic() {
Intent intent = new Intent(this, GcmIntentService.class);
intent.putExtra(GcmIntentService.KEY, GcmIntentService.SUBSCRIBE);
intent.putExtra(GcmIntentService.TOPIC, Config.TOPIC_GLOBAL);
startService(intent);
}
// Subscribing to all chat room topics
// each topic name starts with `topic_` followed by the ID of the chat room
// Ex: topic_1, topic_2
/*private void subscribeToAllTopics() {
for (ChatRoom cr : chatRoomArrayList) {
Intent intent = new Intent(this, GcmIntentService.class);
intent.putExtra(GcmIntentService.KEY, GcmIntentService.SUBSCRIBE);
intent.putExtra(GcmIntentService.TOPIC, "topic_" + cr.getId());
startService(intent);
}
}*/
private void launchLoginActivity() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();
}
#Override
protected void onResume() {
super.onResume();
// register GCM registration complete receiver
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.REGISTRATION_COMPLETE));
// register new push message receiver
// by doing this, the activity will be notified each time a new message arrives
LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReceiver,
new IntentFilter(Config.PUSH_NOTIFICATION));
// clearing the notification tray
NotificationUtils.clearNotifications();
}
#Override
protected void onPause() {
LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReceiver);
super.onPause();
}
// starting the service to register with GCM
private void registerGCM() {
Intent intent = new Intent(this, GcmIntentService.class);
intent.putExtra("key", "register");
startService(intent);
}
private boolean checkPlayServices() {
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(this);
if (resultCode != ConnectionResult.SUCCESS) {
if (apiAvailability.isUserResolvableError(resultCode)) {
apiAvailability.getErrorDialog(this, resultCode, PLAY_SERVICES_RESOLUTION_REQUEST)
.show();
} else {
Log.i(TAG, "This device is not supported. Google Play Services not installed!");
Toast.makeText(getApplicationContext(), "This device is not supported. Google Play Services not installed!", Toast.LENGTH_LONG).show();
finish();
}
return false;
}
return true;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.action_logout:
MyApplication.getInstance().logout();
break;
}
return super.onOptionsItemSelected(menuItem);
}
}
ChatRoomsAdapter:
package com.wipro.chat.adapter;
/**
* Created by COMP on 16-06-2016.
*/
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import com.wipro.chat.R;
import com.wipro.chat.model.ChatRoom;
public class ChatRoomsAdapter extends RecyclerView.Adapter<ChatRoomsAdapter.ViewHolder> {
private Context mContext;
private ArrayList<ChatRoom> chatRoomArrayList;
private static String today;
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView name, message, timestamp, count;
public ViewHolder(View view) {
super(view);
name = (TextView) view.findViewById(R.id.name);
message = (TextView) view.findViewById(R.id.message);
timestamp = (TextView) view.findViewById(R.id.timestamp);
count = (TextView) view.findViewById(R.id.count);
}
}
public ChatRoomsAdapter(Context mContext, ArrayList<ChatRoom> chatRoomArrayList) {
this.mContext = mContext;
this.chatRoomArrayList = chatRoomArrayList;
Calendar calendar = Calendar.getInstance();
today = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_rooms_list_row, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
ChatRoom chatRoom = chatRoomArrayList.get(position);
holder.name.setText(chatRoom.getName());
holder.message.setText(chatRoom.getLastMessage());
if (chatRoom.getUnreadCount() > 0) {
holder.count.setText(String.valueOf(chatRoom.getUnreadCount()));
holder.count.setVisibility(View.VISIBLE);
} else {
holder.count.setVisibility(View.GONE);
}
holder.timestamp.setText(getTimeStamp(chatRoom.getTimestamp()));
}
#Override
public int getItemCount() {
return chatRoomArrayList.size();
}
public static String getTimeStamp(String dateStr) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String timestamp = "";
today = today.length() < 2 ? "0" + today : today;
try {
Date date = format.parse(dateStr);
SimpleDateFormat todayFormat = new SimpleDateFormat("dd");
String dateToday = todayFormat.format(date);
format = dateToday.equals(today) ? new SimpleDateFormat("hh:mm a") : new SimpleDateFormat("dd LLL, hh:mm a");
String date1 = format.format(date);
timestamp = date1.toString();
} catch (ParseException e) {
e.printStackTrace();
}
return timestamp;
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private ChatRoomsAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ChatRoomsAdapter.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildLayoutPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildLayoutPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
}
PHP code which is retrieving the chatroom is like:
/* * *
* fetching all chat rooms
*/
$app->get('/chat_rooms', function() {
$response = array();
$db = new DbHandler();
// fetching all user tasks
$result = $db->getAllChats();
$response["error"] = false;
$response["chat_rooms"] = array();
// pushing single chat room into array
while ($chat_room = $result->fetch_assoc()) {
$tmp = array();
$tmp["user_id"] = $chat_room["user_id"];
$tmp["name"] = $chat_room["name"];
$tmp["created_at"] = $chat_room["created_at"];
array_push($response["chat_rooms"], $tmp);
}
echoRespnse(200, $response);
});
public function getAllChats() {
$stmt = $this->conn->prepare("SELECT user_id, name, created_at FROM users");
$stmt->execute();
$tasks = $stmt->get_result();
$stmt->close();
return $tasks;
}
There are two user chats in my database, namely Messaging, Chat and I'm getting the both from database into ArrayList but it is only showing Messaging.
Adapter display:
Response from database:
Check recycler_view in your main layout. The height should be set to "wrap_content".