我正在尝试将我的应用程序与在POSTMAN中正常运行的API连接,但不能在我的Android APP中运行。
这是我的代码。我的设备上有互联网许可和互联网。网址和参数也很好。
void loginTask()
{
RequestQueue queue = Volley.newRequestQueue(this);
String url = URLS.url_login;
pd = new ProgressDialog(LoginActivity.this);
pd.setMessage("Ingresando");
pd.show();
app.L("url : "+url);
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
app.L(response);
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
pd.dismiss();
error.printStackTrace();
app.LE("error => "+error.toString());
}
}
) {
public String getBodyContentType()
{
return "application/json";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
// this is the relevant method
@Override
public byte[] getBody()
{
return getJSONBody().toString().getBytes();
}
};
queue.add(postRequest);
}
private String getJSONBody()
{
JSONObject body = null;
try
{
body = new JSONObject();
body.put(URLS.param_username,editText_user.getText().toString());
body.put(URLS.param_pass,editText_pass.getText().toString());
}
catch (Exception ex)
{
ex.printStackTrace();
}
return body;
}
这是错误
BasicNetwork.performRequest: Unexpected response code 404 for http://200.51.42.196:11440/stocktaller/public_html/client/login
com.android.volley.ServerError
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:163)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)
有任何帮助吗?非常感谢!我会尝试其他解决方案
更新:我尝试使用相同结果的JSONRequest
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, getJSONBody(), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
pd.dismiss();
app.L(response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error)
{
pd.dismiss();
error.printStackTrace();
app.LE("error => "+error.toString());
}
})
{
public String getBodyContentType()
{
return "application/json";
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
queue.add(jsonObjectRequest);