EliasMP EliasMP - 2 months ago 15
Android Question

Can´t reach WCF´s proyect in local

After Following a few tuto I programmed this code, which is not working. When I call the activity which contains Retrofit called, app crashed (that happens when I add the code for creating Retrofit object)....
I´m trying to consume a WCF (URL=http://192.168.0.156:52896/Service1.svc) (method´s name=devolverPisosA())

-- Interface --
public interface RestApi {
@GET("/devolverPisosA")
Call<List<pisoAlquiler>> devolverPisosA();
}

-- Pojo Class --
public class pisoAlquiler {
private int codigo;
private String fotos;
// GET AND SET INCLUDED...
}

-- Activity --
public class Alquiler extends AppCompatActivity implements Callback<List<pisoAlquiler>> {

public static final String BASE_URL = "http://192.168.0.156:52896/Service1.svc";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alquiler);

// CREATE CLIENT SERVICE
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();

// prepare call in Retrofit 2.0
RestApi restApi = retrofit.create(RestApi.class);

Call<List<pisoAlquiler>> call = restApi.devolverPisosA();
//asynchronous call
call.enqueue(this);
}


@Override
public void onResponse(Call<List<pisoAlquiler>> call, Response<List<pisoAlquiler>> response) {
setProgressBarIndeterminateVisibility(false);

responsePisosAlquiler = (ArrayList<pisoAlquiler>) response.body();
}

@Override
public void onFailure(Call<List<pisoAlquiler>> call, Throwable t) {
Toast.makeText(Alquiler.this, t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}


So my main_activity is calling (and starting a new activity) a second activity (which launch retrofit), but I never got to reach it. Second activity returns to main_activity instantly without doing anything (retrofit stuff)...

Thanks Mates...

EDITED:
The problem (and I´m guessing) could be my definition of endpoints. When I´m setting retrofit with baseUrl and get... My WCF looks like http://192.168.0.156:52896/Service1.svc and the method which I want to call is devolverPisosA, but I don´t know how to set retrofit for consuming it.

Just trying

baseUrl = http://192.168.0.156:52896/Service1.svc/
@GET("devolverPisosA")

Answer

Well,

The problem was I´m in Debbug Mode and WCF (by Visual Studio) and Android Studio wasn´t configurated in the same network.

For configuring Visual Studio for working with Android Studio in a Local Network, following the next:

Web.Config must to be set for listening from differents places. Between services and Behavior tags:

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

Open applicationhost.config allocated in Project Folder --> Config: Change the next one:

<site name="Proyect" id="2">
            <application path="/" applicationPool="Clr4IntegratedAppPool">
                <virtualDirectory path="/" physicalPath="c:\ProjectFolder\Proyect />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:52896:localhost" />
            </bindings>
        </site>

For:

<site name="Proyect" id="2">
            <application path="/" applicationPool="Clr4IntegratedAppPool">
                <virtualDirectory path="/" physicalPath="c:\ProjectFolder\Proyect" />
            </application>
            <bindings>
                <binding protocol="http" bindingInformation="*:52896:localhost" />
              <binding protocol="http" bindingInformation="*:52896:*" />
            </bindings>
        </site>

With this, We can listen internal request.

Open Windows Firewall for that port.

Comments