【发布时间】:2017-01-20 11:56:16
【问题描述】:
我有两个问题:
为什么我的 URL 为空?
我的 localhost 是否正确连接到我的 Android Studio 模拟器?
如果没有,那么如何将我的 Android Studio 连接到我的本地主机?
MainActivity.java
import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;
public class MainActivity extends AppCompatActivity {
String url = "http://10.0.2.2:8000/";
TextView text_name_1, text_city_1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text_name_1 = (TextView) findViewById(R.id.text_name_1);
text_name_1 = (TextView) findViewById(R.id.text_city_1);
Button ButtonArray= (Button) findViewById(R.id.RetrofitArray);
ButtonArray.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View VisibleArray = findViewById(R.id.RetrofitArray);
VisibleArray.setVisibility(View.GONE);
getRetrofitArray();
}
});
}
void getRetrofitArray() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitArrayAPI service = retrofit.create(RetrofitArrayAPI.class);
Call<List<Healthinfo>> call = service.getHealthDetails();
call.enqueue(new Callback<List<Healthinfo>>() {
@Override
public void onResponse(Response<List<Healthinfo>> response, Retrofit retrofit) {
try {
List<Healthinfo> StudentData = response.body();
for (int i = 0; i < StudentData.size(); i++) {
if (i == 0) {
text_name_1.setText("UserName : " + StudentData.get(i). getUsername());
text_city_1.setText("city : " + StudentData.get(i).getCity());
}
}
} catch (Exception e) {
Log.d("onResponse", "There is an error");
e.printStackTrace();
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", t.toString());
}
});
}
}
RetorfitArrayAPI
import retrofit.http.GET;
public interface RetrofitArrayAPI {
/*
* Retrofit get annotation with our URL
* And our method that will return us details of student.
*/
@GET("/try/salma/123")
Call<List<Healthinfo>> getHealthDetails();
}
我已经在 postman 中检查了我的 REST API,我得到了以下输出:
[{"user_id":"8","username":"salma","email":"salma@hotmail.com","birthday":"2017-01-12","gender":"","skills":"","country":"Pak","city":"Peshawar","number":"8967","password":"123","cpassword":"123","img":""}]
我的 Android 模拟器出现以下错误:
01-20 18:28:34.419 2762-2762/com.example.fahadpirzada.motherhealth I/art: Not late-enabling -Xcheck:jni (already on)
01-20 18:28:34.421 2762-2762/com.example.fahadpirzada.motherhealth I/art: Late-enabling JIT
01-20 18:28:34.445 2762-2762/com.example.fahadpirzada.motherhealth I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
01-20 18:28:34.922 2762-2762/com.example.fahadpirzada.motherhealth W/System: ClassLoader referenced unknown path: /data/app/com.example.fahadpirzada.motherhealth-2/lib/x86
01-20 18:28:35.843 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 836.103ms
01-20 18:28:35.984 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 49.509ms
01-20 18:28:36.340 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 49.543ms
01-20 18:28:36.941 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 234.781ms
01-20 18:28:37.263 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 69.193ms
01-20 18:28:38.013 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 21.492ms
01-20 18:28:38.410 2762-2828/com.example.fahadpirzada.motherhealth D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-20 18:28:38.470 2762-2762/com.example.fahadpirzada.motherhealth D/: HostConnection::get() New Host Connection established 0xa44ab260, tid 2762
01-20 18:28:38.593 2762-2828/com.example.fahadpirzada.motherhealth I/OpenGLRenderer: Initialized EGL, version 1.4
01-20 18:28:38.669 2762-2828/com.example.fahadpirzada.motherhealth W/EGL_emulation: eglSurfaceAttrib not implemented
01-20 18:28:38.669 2762-2828/com.example.fahadpirzada.motherhealth W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xadadf2a0, error=EGL_SUCCESS
01-20 18:28:38.793 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 38.637ms
01-20 18:28:39.266 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 13.926ms
01-20 18:28:42.839 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 58.303ms
01-20 18:28:42.845 2762-2762/com.example.fahadpirzada.motherhealth I/Choreographer: Skipped 36 frames! The application may be doing too much work on its main thread.
01-20 18:28:44.157 2762-2769/com.example.fahadpirzada.motherhealth W/art: Suspending all threads took: 29.957ms
【问题讨论】:
-
我认为您没有设置 baseUrl 这就是发生此错误的原因
-
我已经声明了 "String url = "10.0.2.2:8000"; "
-
然后在@GET中删除localhost:8000
-
还是同样的问题 :(
-
您使用的是哪个版本的改造?
标签: java android rest android-emulator retrofit2