2016-04-14 13 views
1

Hallo Im in einem Test mit Retrofit arbeitet 2,0 und einen des Tests wird eine resquest zu einer URL zu machen, die mit .json beenden:Retrofit 2.0, fordert GET auf eine .json Datei als Endpunkt

Beispiel: https://domain.com/contacts.json

baseURL: https://domain.com/ endPoint: /contacts.json

, die eine Datei ist, aber ich mag eine normale GET-Anfrage machen und die json innen direkt

+0

Können Sie Ihren Quellcode schreiben und erzählen Sie uns mehr über das, was nicht funktioniert? – mbonnin

+0

make base uri 'https: // domain.com /' und end point 'contacts.json' – EpicPandaForce

Antwort

1

Hallo ich eine Lösung gefunden, die Datei mit Ihrem Code zu bekommen und es funktioniert wirklich jetzt, aber ich habe den MIME nicht berühren Ich glaube, auf dem Webserver habe ich den Converter nicht im Code hinzugefügt. Vielen Dank.

WebAPIService.java:

public interface WebAPIService { 
@GET("/contacts.json") 
    Call<JsonObject> getContacts(); 
} 

MainAcitivty.java:

Retrofit retrofit1 = new Retrofit.Builder() 
    .baseUrl(BuildConfig.API_ENDPOINT) 
    .addConverterFactory(GsonConverterFactory.create()) 
    .build(); 

WebAPIService service1 = retrofit1.create(WebAPIService.class); 

Call<List<Contact>> jsonCall = service1.getContacts(); 
jsonCall.enqueue(new Callback<List<Contact>() { 

@Override 
public void onResponse(Call<List<Contact>> call, Response<List<Contact>> response) { 
    Log.i(LOG_TAG, response.body().toString()); 
} 

@Override 
public void onFailure(Call<List<Contact>> call, Throwable t) { 
    Log.e(LOG_TAG, t.toString()); 
} 
}); 
4

Wenn Sie contro haben bekommen l über Ihren Webserver, können Sie anpassen, unterstützt es .json Datei als text/plain oder application/json. Bitte beachten Sie meine folgende Screenshot (Ich habe mit IIS getan 7,5)

enter image description here

Der folgende Screenshot wird eine Anforderung mit PostMan:

enter image description here

build.gradle Datei:

dependencies { 
    ...  
    compile 'com.squareup.retrofit2:retrofit:2.0.1' 
    compile 'com.squareup.retrofit2:converter-gson:2.0.1' 
} 

WebAPIService.java:

public interface WebAPIService { 
    @GET("/files/jsonsample.json") 
    Call<JsonObject> readJson(); 
} 

MainAcitivty.java:

Retrofit retrofit1 = new Retrofit.Builder() 
     .baseUrl("http://...") 
     .addConverterFactory(GsonConverterFactory.create()) 
     .build(); 

WebAPIService service1 = retrofit1.create(WebAPIService.class); 
Call<JsonObject> jsonCall = service1.readJson(); 
jsonCall.enqueue(new Callback<JsonObject>() { 
    @Override 
    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) { 
     Log.i(LOG_TAG, response.body().toString()); 
    } 

    @Override 
    public void onFailure(Call<JsonObject> call, Throwable t) { 
     Log.e(LOG_TAG, t.toString()); 
    } 
}); 

Logcat:

04-15 15:31:31.943 5810-5810/com.example.asyncretrofit I/AsyncRetrofit: {"glossary":{"title":"example glossary","GlossDiv":{"title":"S","GlossList":{"GlossEntry":{"ID":"SGML","SortAs":"SGML","GlossTerm":"Standard Generalized Markup Language","Acronym":"SGML","Abbrev":"ISO 8879:1986","GlossDef":{"para":"A meta-markup language, used to create markup languages such as DocBook.","GlossSeeAlso":["GML","XML"]},"GlossSee":"markup"}}}}} 
+1

Hallo danke dafür ist genau mein Problem, aber ich habe keine Kontrolle über den Webserver, also die Option werde ich gehen Lade die Datei herunter und analysiere sie. – Ivor