我有一个django服务器,但是在Android模拟器中应用程序崩溃了,为什么?
是代码:
final TextView t = findViewById(R.id.textView3);
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://10.0.2.2:8000/";
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
t.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
t.setText("That didn't work!");
}
});
答案 0 :(得分:0)
如果服务器在模拟器上的localhost和app上运行,那么你必须在app中将其他IP作为BaseUrl。
步骤: - &GT;打开终端,输入ifconfig(在mac os上) - &GT;复制en0中显示的inet IP地址,并在应用程序中将其设置为BaseUrl
答案 1 :(得分:0)
可能重复How do you connect localhost in the Android emulator?
如果Genymotion
,请尝试10.0.3.2
答案 2 :(得分:0)
使用您的计算机实际IP地址!用系统实际IP替换10.0.2.2,检查java.net.ConnectException: failed to connect to /10.0.0.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)