Pergunta

I wrote this code that will pull everything from my entity "ContactInfo" from the app engine datastore and place it into a string.

My question is how do I make specific queries in the android endpoint. I only want to display the items in the entity that are a certain name or zipcode for example. Here is what I have to put the entire entity into a string.

package com.indeeditis;

import java.io.IOException;



import java.util.Date;

import org.json.JSONException;

import android.os.AsyncTask;
import android.content.Context;
import android.content.Intent;

import com.indeeditis.MainActivity.EndpointsTask;
import com.indeeditis.contactinfoendpoint.Contactinfoendpoint;
import com.indeeditis.contactinfoendpoint.model.CollectionResponseContactInfo;
import com.indeeditis.contactinfoendpoint.model.ContactInfo;

import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.http.HttpRequest;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.json.jackson.JacksonFactory;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.app.Activity;
import android.view.View.OnClickListener;


public class FinderActivity extends Activity  implements OnClickListener {





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

    Button start = (Button)findViewById(R.id.button9000);
    start.setOnClickListener(this);


}



public class EndpointsTask extends AsyncTask<Context, Integer, Long> {




    protected Long doInBackground(Context... contexts) {

      Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
          AndroidHttp.newCompatibleTransport(),
          new JacksonFactory(),
          new HttpRequestInitializer() {
          public void initialize(HttpRequest httpRequest) { }
          });
  Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
  endpointBuilder).build();





  try {

     String apples = endpoint.listContactInfo().execute().toString();



        Log.w("myApp", apples);



  } catch (IOException e) {
    e.printStackTrace();
  }
      return (long) 0;
    }




    }



@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

    new EndpointsTask().execute(getApplicationContext());

}





}
Foi útil?

Solução

For applying filters while sending a query request from android through cloud endpoints, you need to add options to set parameters. I will explain with a short example

1, Assume your contactinfo endpoint has 2 properties

name = ndb.StringProperty()
contacttype = ndb.StringProperty() // assume this can have values personal or business

2, Now if you need to query for a particular contacttype, then your app engine code should have a query method that filters based on the property contacttype. Your endpoint should call this query method by passing on the input parameter from the user.

3, Now to send the required parameter from android , your class Listcontactinfo where you would define the REST Path and method type, should include an option to the set the contacttype parameter

@com.google.api.client.util.Key
private String contacttype;


public String getContacttype() {
    return contacttype;
  }

  public Listcontactinfo setContacttype(String contacttype) {
    this.contacttype = contacttype;
    return this;
  }

4, Finally while calling the endpoint method from your android code, you should pass a value using the setContacttype, which will be something like:

String apples = endpoint.listContactInfo().setContacttype("personal").execute().toString();

This is for an example case where you want to query entities having contacttype with value "personal"

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top