0

i'm newbie using retrofit,i have error my code not work below my code to receive data from localhost using retrofit with mysql and php , it does not work because it's entered in onfailure case any help please and thanks. php woking well and its' json well .

ApiClient.java

package com.example.alaa.retrofiapiex;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Ala'a on 7/21/2017.
 */

public class ApiClient {
    public static final String Base_URL="https://10.0.2.2/";
    public  static Retrofit retrofit;

    public  static Retrofit getApiClient()
    {
        if(retrofit==null)
        {
            retrofit=new Retrofit.Builder().baseUrl(Base_URL).
                    addConverterFactory(GsonConverterFactory.create()).build();
        }
        return retrofit;
    }
}

ApiInterface.java

package com.example.alaa.retrofiapiex;

import java.util.List;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by Ala'a on 7/21/2017.
 */

public interface ApiInterface {

    @GET("getcalories.php")
    Call<List<calories>> getCaloriesInfo(@Query("item_type") String item_type);
}

here where i write the fuctoin in recycler view, and function to call retrofit where it's enter in onFailure. ** ListActivity.java **

package com.example.alaa.retrofiapiex;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ListActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private List<calories> calories;
    private  RecyclerAdapter adapter;
    private ApiInterface apiInterface;


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

        recyclerView= (RecyclerView) findViewById(R.id.recycleview);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);

        if(getIntent().getExtras()!=null)
        {
            String type=getIntent().getExtras().getString("type");
            fetchInformation(type);
        }
    }

    private void fetchInformation(String type) {
      apiInterface=ApiClient.getApiClient().create(ApiInterface.class);
        Call<List<calories>> call=apiInterface.getCaloriesInfo(type);
        call.enqueue(new Callback<List<calories>>() {
                         @Override
                         public void onResponse(Call<List<calories>> call, Response<List<calories>> response) {
                             calories=response.body();
                             adapter=new RecyclerAdapter(calories,ListActivity.this);
                             recyclerView.setAdapter(adapter);
                             Toast.makeText(getApplicationContext(), " On Success message!",
                                     Toast.LENGTH_LONG).show();
                         }

                         @Override
                         public void onFailure(Call<List<calories>> call, Throwable t) {
                             Toast.makeText(getApplicationContext(), " On Failure message!",
                                     Toast.LENGTH_LONG).show();
                         }
                     }

        );

    }
}

calories.java

package com.example.alaa.retrofiapiex;

import com.google.gson.annotations.SerializedName;

/**
 * Created by Ala'a on 7/21/2017.
 */

public class calories {
    @SerializedName("name")
    private  String Name;

    @SerializedName("image")
    private  String Image;

    @SerializedName("calories")
    private  String Calories;

    public String getName() {
        return Name;
    }
    public String getImage() {
        return Image;
    }

    public String getCalories() {
        return Calories;
    }

}

RecyclerAdapter.java

package com.example.alaa.retrofiapiex;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by Ala'a on 7/21/2017.
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private List<calories> caloriesList;
    private Context context;

    public RecyclerAdapter(List<calories> caloriesList, Context context) {
        this.caloriesList = caloriesList;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.raw_layout,parent,false);


        return new MyViewHolder(view);

    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.Name.setText(caloriesList.get(position).getName());
        holder.calories.setText(caloriesList.get(position).getCalories());
        Glide.with(context).load(caloriesList.get(position).getImage()).into(holder.img);



    }

    @Override
    public int getItemCount() {
        return caloriesList.size();
    }

    public  static class MyViewHolder extends RecyclerView.ViewHolder
    {
        ImageView img;
        TextView Name, calories;
        public MyViewHolder(View itemView) {
            super(itemView);
            img = (ImageView)itemView.findViewById(R.id.imageView);
            Name= (TextView) itemView.findViewById(R.id.nametxt);
            calories = (TextView) itemView.findViewById(R.id.caloritxt);

        }
    }
}
  • "localhost" is the Android device or emulator, which is probably not what you are meaning to have your Android code talk to. – Chris Stratton Jul 23 '17 at 20:02
  • in test i'm using android device – alaa deego Jul 23 '17 at 20:05
  • 1
    Chris Stratton , why you marked my questiion , i'm not ask how to access localhost, i'm aske that i have error, pleas read the question befor . – alaa deego Jul 24 '17 at 02:37
  • The point and root cause of your failure is that you simple *cannot* access "localhost". And no, you can't access it as `https://10.0.2.2/` as in your code either, because that only works on Android emulators, not physical android devices. Those are the points made at the duplicate link, which is why this is a duplicate. Even if this were not a duplicate, your question would still be closed, as you fail to state the precise nature of the failure, which is a requirement here for debugging questions. – Chris Stratton Jul 24 '17 at 02:53
  • ooh .. sorry. know i understand. thanks – alaa deego Jul 24 '17 at 04:29

0 Answers0