Fala galera, não estou conseguindo realizar a busca de uma lista de um endpoint no google app engine.
Segue as classes:
Endpoint:
package com.example.gregmachado.panapp.backend;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.api.server.spi.response.CollectionResponse;
import com.google.api.server.spi.response.NotFoundException;
import com.google.appengine.api.datastore.Cursor;
import com.google.appengine.api.datastore.QueryResultIterator;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.cmd.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.annotation.Nullable;
import javax.inject.Named;
import static com.googlecode.objectify.ObjectifyService.ofy;
/**
* WARNING: This generated code is intended as a sample or starting point for using a
* Google Cloud Endpoints RESTful API with an Objectify entity. It provides no data access
* restrictions and no data validation.
* <p/>
* DO NOT deploy this code unchanged as part of a real application to real users.
*/
@Api(
name = "bakeryApi",
version = "v1",
resource = "bakery",
namespace = @ApiNamespace(
ownerDomain = "backend.panapp.gregmachado.example.com",
ownerName = "backend.panapp.gregmachado.example.com",
packagePath = ""
)
)
public class BakeryEndpoint {
private static final Logger logger = Logger.getLogger(BakeryEndpoint.class.getName());
private static final int DEFAULT_LIST_LIMIT = 20;
static {
// Typically you would register this inside an OfyServive wrapper. See: https://code.google.com/p/objectify-appengine/wiki/BestPractices
ObjectifyService.register(Bakery.class);
}
/**
* Returns the {@link Bakery} with the corresponding ID.
*
* @param bakeryId the ID of the entity to be retrieved
* @return the entity with the corresponding ID
* @throws NotFoundException if there is no {@code Bakery} with the provided ID.
*/
@ApiMethod(
name = "get",
path = "bakery/{bakeryId}",
httpMethod = ApiMethod.HttpMethod.GET)
public Bakery get(@Named("bakeryId") Long bakeryId) throws NotFoundException {
logger.info("Getting Bakery with ID: " + bakeryId);
Bakery bakery = ofy().load().type(Bakery.class).id(bakeryId).now();
if (bakery == null) {
throw new NotFoundException("Could not find Bakery with ID: " + bakeryId);
}
return bakery;
}
/**
* Inserts a new {@code Bakery}.
*/
@ApiMethod(
name = "insert",
path = "bakery",
httpMethod = ApiMethod.HttpMethod.POST)
public Bakery insert(Bakery bakery) {
// Typically in a RESTful API a POST does not have a known ID (assuming the ID is used in the resource path).
// You should validate that bakery.bakeryId has not been set. If the ID type is not supported by the
// Objectify ID generator, e.g. long or String, then you should generate the unique ID yourself prior to saving.
//
// If your client provides the ID then you should probably use PUT instead.
ofy().save().entity(bakery).now();
logger.info("Created Bakery with ID: " + bakery.getBakeryId());
return ofy().load().entity(bakery).now();
}
/**
* Updates an existing {@code Bakery}.
*
* @param bakeryId the ID of the entity to be updated
* @param bakery the desired state of the entity
* @return the updated version of the entity
* @throws NotFoundException if the {@code bakeryId} does not correspond to an existing
* {@code Bakery}
*/
@ApiMethod(
name = "update",
path = "bakery/{bakeryId}",
httpMethod = ApiMethod.HttpMethod.PUT)
public Bakery update(@Named("bakeryId") Long bakeryId, Bakery bakery) throws NotFoundException {
// TODO: You should validate your ID parameter against your resource's ID here.
checkExists(bakeryId);
ofy().save().entity(bakery).now();
logger.info("Updated Bakery: " + bakery);
return ofy().load().entity(bakery).now();
}
/**
* Deletes the specified {@code Bakery}.
*
* @param bakeryId the ID of the entity to delete
* @throws NotFoundException if the {@code bakeryId} does not correspond to an existing
* {@code Bakery}
*/
@ApiMethod(
name = "remove",
path = "bakery/{bakeryId}",
httpMethod = ApiMethod.HttpMethod.DELETE)
public void remove(@Named("bakeryId") Long bakeryId) throws NotFoundException {
checkExists(bakeryId);
ofy().delete().type(Bakery.class).id(bakeryId).now();
logger.info("Deleted Bakery with ID: " + bakeryId);
}
/**
* List all entities.
*
* @param cursor used for pagination to determine which page to return
* @param limit the maximum number of entries to return
* @return a response that encapsulates the result list and the next page token/cursor
*/
@ApiMethod(
name = "list",
path = "bakery",
httpMethod = ApiMethod.HttpMethod.GET)
public CollectionResponse<Bakery> list(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
Query<Bakery> query = ofy().load().type(Bakery.class).limit(limit);
if (cursor != null) {
query = query.startAt(Cursor.fromWebSafeString(cursor));
}
QueryResultIterator<Bakery> queryIterator = query.iterator();
List<Bakery> bakeryList = new ArrayList<Bakery>(limit);
while (queryIterator.hasNext()) {
bakeryList.add(queryIterator.next());
}
return CollectionResponse.<Bakery>builder().setItems(bakeryList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
}
private void checkExists(Long bakeryId) throws NotFoundException {
try {
ofy().load().type(Bakery.class).id(bakeryId).safe();
} catch (com.googlecode.objectify.NotFoundException e) {
throw new NotFoundException("Could not find Bakery with ID: " + bakeryId);
}
}
@ApiMethod(
name = "getBakeryByFantasyName",
path = "bakery/fantasyName={fantasyName}",
httpMethod = ApiMethod.HttpMethod.GET)
public Bakery getBakeryByFantasyName(@Named("fantasyName") String fantasyName)throws NotFoundException {
logger.info("Getting Bakery name Fantasy Name: " + fantasyName);
Bakery bakery = ofy().load().type(Bakery.class).filter("fantasyName >=", fantasyName).filter("fantasyName <", fantasyName).first().now();
if (bakery == null) {
throw new NotFoundException("Could not find Bakery with Fantasy Name: " + fantasyName);
}
return bakery;
}
}
Fragment que chama a lista
package com.gregmachado.panapp;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.example.gregmachado.panapp.backend.bakeryApi.BakeryApi;
import com.example.gregmachado.panapp.backend.bakeryApi.model.Bakery;
import com.example.gregmachado.panapp.backend.bakeryApi.model.CollectionResponseBakery;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.gregmachado.panapp.adapter.BakeryAdapter;
import com.gregmachado.panapp.interfaces.RecyclerViewOnClickListenerHack;
import java.io.IOException;
import java.util.List;
/**
* Created by gregmachado on 07/05/16.
*/
public class BakeryFragment extends android.support.v4.app.Fragment implements RecyclerViewOnClickListenerHack {
public static final String APP_NAME = "PanApp";
public static final String URL = "https://panapp-backend.appspot.com/_ah/api";
private static final String TAG = BakeryFragment.class.getSimpleName();
private RecyclerView recyclerView;
private BakeryAdapter bakeryAdapter;
private ProgressDialog progressDialog;
private CollectionResponseBakery bakerys = null;
public BakeryFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_bakery_list, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycler_view_bakery_list);
recyclerView.setHasFixedSize(true);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(bakeryAdapter);
recyclerView.addOnItemTouchListener(new RecyclerViewTouchListener(getActivity(), recyclerView, this));
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
//llm.setReverseLayout(true);
recyclerView.setLayoutManager(llm);
/*GridLayoutManager llm = new GridLayoutManager(getActivity(), 3, GridLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(llm);*/
/*StaggeredGridLayoutManager llm = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
llm.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
mRecyclerView.setLayoutManager(llm);*/
loadsList();
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
private void loadsList() {
progressDialog = ProgressDialog.show(getActivity(), "Carregando lista...", "Aguarde um momento", true, false);
new AsyncTask<String, Void, Boolean>() {
@Override
protected Boolean doInBackground(String... params) {
String content = params[0];
BakeryApi.Builder bakeryBuilder = new BakeryApi.Builder(AndroidHttp.newCompatibleTransport(),
new AndroidJsonFactory(), null).setRootUrl(URL);
bakeryBuilder.setApplicationName(APP_NAME);
BakeryApi bakeryService = bakeryBuilder.build();
try {
bakerys = bakeryService.list().execute();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
return false;
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if (result) {
List <Bakery> bakeryList = bakerys.getItems();
for (Bakery bakery : bakeryList) {
Bakery bakeryAux = new Bakery();
bakeryAux.setFantasyName(bakery.getFantasyName());
bakeryAux.setStreet(bakery.getStreet());
bakeryList.add(bakeryAux);
}
BakeryAdapter adapter = new BakeryAdapter(getActivity(), bakeryList);
adapter.setRecyclerViewOnClickListenerHack(BakeryFragment.this);
recyclerView.setAdapter( adapter );
bakeryAdapter.notifyDataSetChanged();
progressDialog.dismiss();
} else {
progressDialog.dismiss();
Toast.makeText(getActivity(), "Erro ao carregar lista de padarias!" , Toast.LENGTH_SHORT).show();
}
}
}.execute();
}
@Override
public void onClickListener(View view, int position) {
}
@Override
public void onLongPressClickListener(View view, int position) {
}
private static class RecyclerViewTouchListener implements RecyclerView.OnItemTouchListener {
private Context mContext;
private GestureDetector mGestureDetector;
private RecyclerViewOnClickListenerHack mRecyclerViewOnClickListenerHack;
public RecyclerViewTouchListener(Context c, final RecyclerView rv, RecyclerViewOnClickListenerHack rvoclh) {
mContext = c;
mRecyclerViewOnClickListenerHack = rvoclh;
mGestureDetector = new GestureDetector(mContext, new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(MotionEvent e) {
super.onLongPress(e);
View cv = rv.findChildViewUnder(e.getX(), e.getY());
if (cv != null && mRecyclerViewOnClickListenerHack != null) {
mRecyclerViewOnClickListenerHack.onLongPressClickListener(cv,
rv.getChildPosition(cv));
}
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
View cv = rv.findChildViewUnder(e.getX(), e.getY());
if (cv != null && mRecyclerViewOnClickListenerHack != null) {
mRecyclerViewOnClickListenerHack.onClickListener(cv,
rv.getChildPosition(cv));
}
return (true);
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
mGestureDetector.onTouchEvent(e);
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HomeFragment"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:padding="10dp">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view_bakery_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</RelativeLayout>