2017-06-27 5 views
0

私は過去数日からRetrofitの導入を学びましたが、いくつかの問題に直面しています。誰かに助けを借りて助けてください?改訂履歴helpFailed

public class MainActivity extends AppCompatActivity { 

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

     Retrofit.Builder builder = new Retrofit.Builder().baseUrl("https://query.yahooapis.com")         .addConverterFactory(GsonConverterFactory.create()); 
     Retrofit retrofit = builder.build(); 
     Details client = retrofit.create(Details.class); 

     Call<ArrayList<Condition>> call = client.reposForUser(); 
     call.enqueue(new Callback<ArrayList<Condition>>() { 
      @Override 
      public void onResponse(Call<ArrayList<Condition>> call, Response<ArrayList<Condition>> response) { 
       Toast.makeText(MainActivity.this, "Yes", Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onFailure(Call<ArrayList<Condition>> call, Throwable t) { 

       Toast.makeText(MainActivity.this, "NO", Toast.LENGTH_SHORT).show(); 
       System.out.print(t.getStackTrace().toString()); 
      } 
     }); 


    } 
} 

/////////////////////////////////////////////////////////////////////// /////////////////////////////////

public interface Details { 


    @GET("/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22New%20Delhi%2CIndia%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys") 
    Call<ArrayList<Condition>> reposForUser(); 

} 

////////// ///////////////////////////////////////////////////////////////// ///////////////////

package hawkeyestudios.weatherretro; 

import com.google.gson.annotations.Expose; 
import com.google.gson.annotations.SerializedName; 

public class Condition { 

    @SerializedName("code") 
    @Expose 
    private String code; 
    @SerializedName("date") 
    @Expose 
    private String date; 
    @SerializedName("temp") 
    @Expose 
    private String temp; 
    @SerializedName("text") 
    @Expose 
    private String text; 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 

    public String getTemp() { 
     return temp; 
    } 

    public void setTemp(String temp) { 
     this.temp = temp; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 
} 
+0

作りますd?いつも "onFailure"?あなたはlogcatまたは "System.out.print(t.getStackTrace()。toString());"の出力を提供できますか? ? – BenRoob

+0

'Conditiont'フィールドはOKですか? URLに続いてコード、日付、温度などが表示されます。 –

答えて

0

私はそれらのJSONに基づいて、あなたのAPIへの呼び出しを行った、私はセグメントを持ってobject.Thisは、あなたのレスポンスクラスですレスポンスcをリクエストできる場合は、json全体を保持します少女は、あなたのJSON

public class Response { 

    @SerializedName("query") 
    @Expose 
    private Query query; 

    public Query getQuery() { 
     return query; 
    } 

    public void setQuery(Query query) { 
     this.query = query; 
    } 

} 

------------結果クラス------

public class Query { 


    @SerializedName("results") 
    @Expose 
    private Results results; 

    public Results getResults() { 
     return results; 
    } 

    public void setResults(Results results) { 
     this.results = results; 
    } 

} 

を保持しているQueryクラスを得ることができます - ------チャンネルクラス

public class Results { 

    @SerializedName("channel") 
    @Expose 
    private Channel channel; 

    public Channel getChannel() { 
     return channel; 
    } 

    public void setChannel(Channel channel) { 
     this.channel = channel; 
    } 

} 

を保持している結果クラスは---これは、Itemクラス

public class Channel { 


    @SerializedName("item") 
    @Expose 
    private Item item; 



    public Item getItem() { 
     return item; 
    } 

    public void setItem(Item item) { 
     this.item = item; 
    } 

} 
に保持しているChannelクラス

----これは、アレイが、あなたのJSONレスポンスに基づいて単一のオブジェクトではありません、あなたの実際のデータの条件を保持しているItemクラス---最後に

public class Item { 


    @SerializedName("condition") 
    @Expose 
    private Condition condition; 



    public Condition getCondition() { 
     return condition; 
    } 

    public void setCondition(Condition condition) { 
     this.condition = condition; 
    } 



} 

-----条件でありますクラス

public class Condition { 

    @SerializedName("code") 
    @Expose 
    private String code; 
    @SerializedName("date") 
    @Expose 
    private String date; 
    @SerializedName("temp") 
    @Expose 
    private String temp; 
    @SerializedName("text") 
    @Expose 
    private String text; 

    public String getCode() { 
     return code; 
    } 

    public void setCode(String code) { 
     this.code = code; 
    } 

    public String getDate() { 
     return date; 
    } 

    public void setDate(String date) { 
     this.date = date; 
    } 

    public String getTemp() { 
     return temp; 
    } 

    public void setTemp(String temp) { 
     this.temp = temp; 
    } 

    public String getText() { 
     return text; 
    } 

    public void setText(String text) { 
     this.text = text; 
    } 

} 

は//だから、最終的には正確に「常にfaileは何何らかの変化を

call<Response> call = client.reposForUser(); 
関連する問題