gpt4 book ai didi

java - Android 截击 : Headers are not appended

转载 作者:行者123 更新时间:2023-12-02 11:48:29 25 4
gpt4 key购买 nike

我正在尝试在我的 Android 应用程序中实现标记化,为此,每当我向服务器发出请求时,我总是需要将授权 header 添加到我的请求中。问题是,当我调试服务器实例时,即使发出请求,也没有在其中添加授权 header

这是我的代码

private void syncDatabases()
{
String tag_json_obj = "json_obj_req";
String uri = "random_url";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait while syncing");
pDialog.show();

List<Plane> allPlane = service.gettAllPlane();
Gson gson = new Gson();
JSONArray jsArray = new JSONArray();

for (Plane p : allPlane)
{
String jsonString = gson.toJson(p);
try {
JSONObject obj = new JSONObject(jsonString);
obj.remove("ID");
jsArray.put(obj);

} catch (JSONException e) {
e.printStackTrace();
}
}

JsonArrayRequest jsonObjReq = new JsonArrayRequest(Request.Method.POST, uri, jsArray, new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
ArrayList<Plane> newList = new ArrayList<>();
try
{
for(int i=0;i<response.length();i++)
{
String planeName,planeEngine,planeProducer,planeCountry,planeYear,wikiLink,ID;
JSONObject pl = response.getJSONObject(i);

String p1 = pl.optString("ID");
if (pl != null && !p1.isEmpty())
ID = pl.getString("ID");
else
ID = "0";

String p2 = pl.optString("planeName");
if (p2 != null && !p2.isEmpty())
planeName = pl.getString("planeName");
else
planeName = "";

String p3 = pl.optString("planeEngine");
if (p3 != null && !p3.isEmpty())
planeEngine = pl.getString("planeEngine");
else
planeEngine = "";

String p4 = pl.optString("planeProducer");
if (p4 != null && !p4.isEmpty())
planeProducer = pl.getString("planeProducer");
else
planeProducer = "";

String p5 = pl.optString("planeCountry");
if (p5 != null && !p5.isEmpty())
planeCountry = pl.getString("planeCountry");
else
planeCountry = "";

String p6 = pl.optString("planeYear");
if (p6 != null && !p6.isEmpty())
planeYear = pl.getString("planeYear");
else
planeYear = "0";

String p7 = pl.optString("wikiLink");
if (p7 != null && !p7.isEmpty())
wikiLink = pl.getString("wikiLink");
else
wikiLink = "";

Plane plf = new Plane(Integer.parseInt(ID),planeName,planeEngine,planeProducer,planeCountry,Integer.parseInt(planeYear),wikiLink);
newList.add(plf);
}
}
catch (JSONException e)
{
e.printStackTrace();
}

service.deleteAllFromPlane();
for (Plane p : newList)
{
boolean ok = service.addNewPlane(p);
}
pDialog.dismiss();
tab1.onIorU();

}

}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.e("ERROR", "Error occurred ", error);
pDialog.dismiss();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
String auth_token_string = settings.getString("token", "");

Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("Authorization", auth_token_string);
return params;
}
};

我已经验证了 auth_token_string 是否为空(不是)。这是我在服务器端看到的内容(C# Web API 2 服务器) enter image description here

这是我获取标题的方法:

var authorization = request.Headers.Authorization;

在图片中,您可以看到我尝试在 header 字段中发送的 token 的一部分。我究竟做错了什么?谢谢。

最佳答案

在您的授权 token 中,您缺少授权类型。我假设您正在使用 Bacis auth,所以尝试使用这个:

params.put("Authorization", "Basic " + auth_token_string);

关于java - Android 截击 : Headers are not appended,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48042575/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com