android


List all users with webservice Restful in Android


I've already spent a lot of months in this code (since last year) and I can't get it done. Now I'm very close, but I still can't list it, and I don't know what else I can do, since I already read/watched all tutorials I know in English and Portuguese. Please someone help.
When I click on the page with the listview, the app crashes. No error is generated in the log.
The webservice is tested and works fine, the problem is in the android code.
My object:
import java.io.Serializable;
public class Imovel implements Serializable {
private int id;
private String prop;
public Imovel() {
}
public Imovel(int id, String prop) {
this.id = id;
this.prop = prop;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProp() {
return prop;
}
public void setProp(String prop) {
this.prop = prop;
}
}
LocacaoFragment (some parts are commented because I don't need to try those already, but I guess it is not interfering)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.List;
import br.com.imobapp.Main.Imovel;
import br.com.imobapp.Main.ImovelService;
import br.com.imobapp.Main.MainActivity;
import br.com.imobapp.R;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
/**
* A simple {#link Fragment} subclass.
*/
public class LocacaoFragment extends Fragment implements View.OnClickListener {
private static final String BASE_URL = "(http://127.0.0.1:8080/ImobWS/webresources/imovel/get/imoveis/";
public LocacaoFragment() {
// Required empty public constructor
}
ImageButton btSearchLoc;
ListView lvLoc;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_locacao, container, false);
btSearchLoc = (ImageButton)view.findViewById(R.id.btSearchLoc);
btSearchLoc.setOnClickListener(this);
lvLoc = (ListView)view.findViewById(R.id.lvLoc);
return view;
}
Gson g = new GsonBuilder().registerTypeAdapter(Imovel.class, new LocacaoDes()).create();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(g))
.build();
private void exibirLocacao() {
ImovelService service = retrofit.create(ImovelService.class);
Call<List<Imovel>> imovel = service.getImoveis();
//faz a chamada assincrona
imovel.enqueue(new Callback<List<Imovel>>() {
#Override
public void onResponse(Call<List<Imovel>> call, Response<List<Imovel>> response) {
if (response.isSuccessful()) {
List<Imovel> imovel = response.body();
for (Imovel im : imovel) {
Log.i("IMOVEL", im.getId() + "\n" + im.getProp());
Log.i("IMOVEL", "-----------------------");
}
} else {
Toast.makeText(getActivity().getApplicationContext(), "Erro: " + response.code(), Toast.LENGTH_LONG).show();
}
}
#Override
public void onFailure(Call<List<Imovel>> call, Throwable t) {
Toast.makeText(getActivity().getApplicationContext(), "Erro: " + t.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
#Override
public void onClick(View view){
MainActivity activity = (MainActivity)getActivity();
/*
switch(view.getId()){
// case R.id.btSearchCor:
//activity.chamaSearchLoc();
// break;
default: throw new RuntimeException("unreachable");
}
*/
}
}
Rest
import android.os.StrictMode;
import android.util.Log;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import java.io.IOException;
public class AcessoRest {
private int TIMEOUT_MILLISEC = 3000;
public String chamadaGet(String url)
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet chamadaget = new HttpGet(url);
String retorno = "";
// Instantiate a GET HTTP method
try {
//Aqui o ideal é colocar a requesição assíncrona
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(chamadaget,
responseHandler);
retorno = responseBody;
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Throwable t) {
Log.i("erro", t.toString());
}
return retorno;
}
}
JsonDeserializer
import com.google.gson.Gson;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
import br.com.imobapp.Main.Imovel;
public class LocacaoDes implements JsonDeserializer {
#Override
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonElement element = json.getAsJsonObject();
if(json.getAsJsonObject() != null){
element = json.getAsJsonObject();
}
return (new Gson().fromJson(element, Imovel.class));
}
}
Interface
import java.util.List;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Path;
public interface ImovelService {
//listar todos imoveis
#GET("get/imoveis")
Call<List<Imovel>> getImoveis();
//buscar imovel por id
#GET("get/imovel/{id}")
Call<Imovel>getImovel(#Path("id") int id);
public static final Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:8080/")
.addConverterFactory(GsonConverterFactory.create())
.build();
}
Dependencies
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
//abaixo para remover erro httpClient
//compile 'cz.msebera.android:httpclient:4.4.1.2'
//compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.code.gson:gson:2.6.2'
compile 'com.squareup.retrofit2:retrofit:2.1.0'
compile 'com.squareup.retrofit2:converter-gson:2.1.0'
compile 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2'
testCompile 'junit:junit:4.12'
}
The Gson Jar is in the program, and internet permission is set.
If you need to see any other code please let me know.

Related Links

Xamarin Android preserve method from Linking
Batch update - in a dialog or a new activity
WebView - onReceivedHttpAuthRequest never stops
How to “RE-SELECT” tab in TabLayout programmatically?
Opening fragment with custom animations and then quickly pressing back causing app to freeze
How to use monkey tool to send customize events?
Android Cannot parse json even though it is valid
How to mix two wav files without noise?
how can i implement the contact list such that if i on click to call these number in the array
get day name from day number of the year android
Android , get ID if the shared calendar
Does ad unit Frequency capping setting counts seen or requested ads?
RecyclerView fit item to bottom
Answer call programatically in Android Marshmallow 6.0
Android indeterminated ProgressBar is not animating
java.lang.NullPointerException:Attempt to invoke virtual method on a null object reference

Categories

HOME
cheerio
c99
google-spreadsheet
solaris
jsf-2.2
android-json
iis-6
extjs4.2
fosrestbundle
gruntfile
microsoft-dynamics-nav
aws-sdk
fileserver
vuejs
template-engine
nim
babel
ember-cli
php-mysqlidb
jboss6.x
mount
jquery-multiselect
browser-sync
gp
carrierwave
nsurl
dotspatial
jmockit
activexobject
reactjs.net
sql-delete
navigator
parse-tree
openproject
google-knowledge-graph
lint
release
filesystemwatcher
c9.io
naming
linqpad
rfc3339
httrack
gitlist
yii2-user
pdf-conversion
flyout
trigger.io
rsa-archer-grc
renaming
sandcastle
materialized-path-pattern
mapxtreme
resourcemanager
jodd
lean
pre-build-event
kiosk
phpwebsocket
thread-sleep
aspen
gocql
rewrite
jdi
poppler
haskell-warp
sip-server
administration
verold
radiant
android-framework
android-audiorecord
mysql-error-1044
merb
jquery-blockui
xcode3.2
lambdaj
apache-whirr
infobright
landscape-portrait
filemerge
dataform
objective-j
addchild
zend-rest
qtabbar
build-environment
asynchronous-wcf-call
application-planning
image-capture
pnrp

Resources

Encrypt Message