我有一个错误。我将数据发送到服务器,但是此代码不起作用。 请帮我。谢谢。我有简单的登录方法。那是可行的,但这是行不通的。错误在哪里。
@Override 受保护的字符串doInBackground(String ... params){
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, new AppData().MUSTERI_KAYIT,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonObj = null;
Log.i(" ADD CUSTOMER RESPONSE - ", response);
} catch (Exception e) {
Log.i(" ADD CUSTOMER RESPONSE ERROR ", e.toString());
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("volley", "Error: " + error.getMessage());
error.printStackTrace();
snackBar();
Log.e(" --- ", error.toString());
}
}) {
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
VolleyError error = new VolleyError(new String(volleyError.networkResponse.data));
volleyError = error;
Log.d(" Errrrorrrrr - ", volleyError.toString());
}
return volleyError;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String auth = "Bearer " + access_token;
params.put("Authorization", auth);
params.put("Content-Type","application/x-www-form-urlencoded");
params.put("accounttype", editMusteriTipi.getText().toString());
params.put("title", editUnvan.getText().toString());
params.put("phone", editPhone.getText().toString());
params.put("email", editEMail.getText().toString());
params.put("tckn", editTcKimlikNo.getText().toString());
params.put("city", editCity.getText().toString());
params.put("district", editIlce.getText().toString());
params.put("addressdetail", editAdress.getText().toString());
//params.put("Vinno", editAracNo.getText().toString());
for (int i = 0; i < sektorList.size(); i++) {
if (sektorList.get(i).getName().equals(editSektor.getText().toString())) {
params.put("sector", sektorList.get(i).getId());
}
}
for (int i = 0; i < kategoriList.size(); i++) {
if (kategoriList.get(i).getName().equals(editKategori.getText().toString())) {
params.put("category", kategoriList.get(i).getId());
}
}
for (int i = 0; i < vergiDairesiList.size(); i++) {
if (vergiDairesiList.get(i).getName().equals(editVergiDairesi.getText().toString())) {
}
}
params.put("vd", editVergiDairesi.getText().toString());
params.put("vdno", editVergiDairesiNo.getText().toString());
return params;
}
};
RequestQueue queue = Volley.newRequestQueue(CustomerRecord.this);
queue.add(jsonObjRequest);
return null;
}
下面的代码是错误的。
E/ ---: com.android.volley.VolleyError: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>401 - Unauthorized: Access is denied due to invalid credentials.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
<h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
</fieldset></div>
</div>
</body>
</html>
并且在错误以下,
E/Volley: [514] BasicNetwork.performRequest: Unexpected response code 401 for http://....
我搜索如何解决此问题,但没有找到我
答案 0 :(得分:1)
/** Your login API response is an HTML error.
1. You should before test the API with Postman if is working properly.
2. Then I suggest you to print on Logcat the request you are doing from you're app. Based on you're code: **/
Log.d("LOGIN_REQ" , AppData().MUSTERI_KAYIT);
StringRequest jsonObjRequest = new StringRequest(Request.Method.POST, new AppData().MUSTERI_KAYIT,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
.....
}
.....
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String auth = "Bearer " + access_token;
params.put("Authorization", auth);
params.put("Content-Type","application/x-www-form-urlencoded");
....
....
Log.d("LOGIN_REQ",params);
return params;
}
};
/**
3. Another think based on my little backend experience you are sending the
Authorization and the Content type as parameters in the request body. The
Authorization and the Content type are headers. So using volley you should
override another method:
**/
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
String auth = "Bearer " + access_token;
params.put("Authorization", auth);
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
答案 1 :(得分:0)
我建议您使用翻新,以避免此类问题。 您将编写更少的代码,但会得到很多。
这是用于实现API的两种主要技术的主要区别是我们的android应用。您可以看到性能上的差异,一次讨论意味着一次网络请求和响应将花费多少时间。
排球
改造
原始帖子here
学习改造的教程
我发现以下教程对于学习翻新基础非常有效