我正在尝试使用Volley执行POST请求,但始终收到错误com.android.volley.NoConnectionError: java.net.UnknownHostException: Unable to resolve host "<my custom API URL>"
。没有与主机名关联的地址。该请求似乎可以使用Postman进行工作,并且我已经在清单中赋予了必要的权限(INTERNET和ACCESS_NETWORK_STATE)。
我正在使用带有SequelPro和Laravel的本地服务器。
这是邮递员的工作屏幕:
Postman body
Postman headers
这是登录活动代码的一部分:
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final EditText emailInput = findViewById(R.id.emailInput);
final EditText passwordInput = findViewById(R.id.passwordInput);
final EditText confirmPasswordInput = findViewById(R.id.confirmPasswordInput);
final Button loginBtn = findViewById(R.id.loginBtn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = emailInput.getText().toString();
final String password = passwordInput.getText().toString();
final String confirmPassword = confirmPasswordInput.getText().toString();
try {
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String URL = "http://lalalaravel.test/api/login";
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", email);
jsonBody.put("password", password);
jsonBody.put("password_confirmation", confirmPassword);
JsonObjectRequest jsonObject = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("VOLLEY", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("VOLLEY", error.toString());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjM4MmM1OTc5YzFhN2Y1ZDczMjFjMWQwMThhYzBmMTdkZWI1MzlkMjIxN2M2YWRhMDUxNzhlMTcyNmVmOWQwZDM3ZGY2ZTdlNDIxOGUxNmQ5In0.eyJhdWQiOiIxIiwianRpIjoiMzgyYzU5NzljMWE3ZjVkNzMyMWMxZDAxOGFjMGYxN2RlYjUzOWQyMjE3YzZhZGEwNTE3OGUxNzI2ZWY5ZDBkMzdkZjZlN2U0MjE4ZTE2ZDkiLCJpYXQiOjE1MjkyNTg2OTAsIm5iZiI6MTUyOTI1ODY5MCwiZXhwIjoxNTYwNzk0NjkwLCJzdWIiOiIyMSIsInNjb3BlcyI6W119.hyjXMXwRb3GLVJ7w5KBmzwVDzoPcztjXKHOx8fqwN6jOvTfgki_HngAIB3JaG5Xhb7w8mFBX03fUDwzOc2S108SiBTjZ7B14GtYmlC8OgxVkKXcCqJ_0UOZBsgCYo7p7f4HBIgIyZ8StrsNlNbuj_4W5Pz2YY4WitbvDX9LsZFIbdz3vhVlmtR2zKA23hyRGUsWZniB7zLGEvsUt5XA9NNAy9XWySaw7JuRZ4uyYm0bXHrJKHSTmfBd9KOJSdpnWs_9miNvh5g_DJo39L7awAlG7-cb67Ih00bhrmgGAWp6VCEd5h-EhIc-l1qfYT56osDgnYIqMk3cAgIWrYrhrs6-mZ1miXqipk9dClliUO7DBylZW8cRbQ5PQ5xisE_wCMTSUr7VYE_7RyjMzDaZuctf8kHQ_W27ED9tq14NhgvJs4hkpO6Gzr32zyMj-KRan2Fc3O1CYgyJx0OR2LhvXrxp2PxfUSIzR8eGHOGypLicU_suG3Dsz_UG7Kp5EdhP8Ma9eDyWObfU64iFYZsuz58TOBOH2ssH7_PpKHBGmchxQSjpKsIwsjeYpdbVmzXG7cZe2_AOEZpXmlwkaAZLow3_kDPfvuoCIWVsEKfgRs72t3B8fIwSt-vWaXpGYKjGOjf9ZOXpiKjG1XHaur0vLR1eiIXKVFSyn6y42FIlbSqk");
headers.put("Content-Type", "application/json");
return headers;
}
};
queue.add(jsonObject);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
老实说,我不知道该怎么办,互联网运行良好,我的一些同学正在与iO进行请求,这没有问题,这应该不是连接问题...