我正在制作FriendManagementApp。我想从本地主机获取响应。我正在使用gson,volley,recyclerview但我没有得到回复。
private void requestJsonObject() throws AuthFailureError
{
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://192.168.1.50:8080/*************/";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
// Log.d(TAG, "Response " + response);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<FriendsData> posts = new ArrayList<FriendsData>();
posts = Arrays.asList(mGson.fromJson(response, FriendsData[].class));
Log.i("MainActivity", posts.size() + " posts loaded.");
for (FriendsData data : posts) {
Log.i("MainActivity", data.getId() + ": " + data.getFirstname() + ": " + data.getLastname() + ":" + data.getContact());
adapter = new RecyclerViewAdapter(MainActivity.this, posts);
//recyclerView.setAdapter(adapter);
// recyclerView.getAdapter().addAll(posts);
// recyclerView.getAdapter().notifyDataSetChaged();
System.out.println(data.getId());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d(TAG, "Error " + error.getMessage());
System.out.print(error.getMessage());
}
});
queue.add(stringRequest);
}
答案 0 :(得分:0)
如果您使用模拟器连接到本地计算机,请使用10.0.2.2
IP地址而不是192.168.1.50
。查看this链接
答案 1 :(得分:0)
我假设您在本地计算机上安装了LAMP服务器。