在Retrofit baseUrl中使用仿真器ip地址10.0.2.2

时间:2016-09-13 14:52:01

标签: java android gson retrofit

这是我的方法我试图连接到我的本地服务器,但它带来一个错误,我正在使用一个模拟器IP地址

错误:(85,17)错误:无法访问HttpUrl 找不到okhttp3.HttpUrl的类文件

 private void loadJSON(){
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://10.0.2.2")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            RequestInterface request = retrofit.create(RequestInterface.class);
            Call<JSONResponse> call = request.getJSON();

            call.enqueue(new Callback<JSONResponse>() {
                @Override
                public void onResponse(Call<JSONResponse> call, retrofit2.Response<JSONResponse> response) {
                    JSONResponse jsonResponse = response.body();
                    flowerList = new ArrayList<>(Arrays.asList(jsonResponse.getNews()));
                    adapter = new NewsAdapter2(MainActivity.this,flowerList);
                    mRecycler.setAdapter(adapter);
                }

                @Override
                public void onFailure(Call<JSONResponse> call, Throwable throwable) {
                    Log.d("Error",throwable.getMessage());
                }
            });


        }

以下是我的界面,其中显示了Url的继续

package oerrride.we.huzykamz.newsfeed;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.Call;
import retrofit2.http.GET;
/**
 * Created by HUZY_KAMZ on 9/13/2016.
 */
public interface RequestInterface {
    @GET("UgandaMedicalAccess/HealthCentreNews/fecth.php")
    Call<JSONResponse> getJSON();

}

1 个答案:

答案 0 :(得分:1)

对于本地服务器,传递您的电脑的IP地址。 您可以在cmd上键入ipconfig命令找到您的电脑的IP。