java - 如何解决“价值
我尝试在标头中的应用程序中将令牌发送到服务器端。但是当我尝试发送令牌时出现此错误
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
我的服务器端是 laravel。
即使我尝试使用utf-8
但
问题描述
我尝试在标头中的应用程序中将令牌发送到服务器端。但是当我尝试发送令牌时出现此错误
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
我的服务器端是 laravel。
即使我尝试使用utf-8
但它没有用
con.addRequestProperty("Authorization", "Bearer " + token+"; charset=utf-8" );
这是我连接到服务器时的代码
@Override
protected Object doInBackground(Object[] objects) {
try {
URL url = new URL(objects[URL_ADDRESS].toString());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(objects[CONNECTION_METHOD].toString());
if (token != null) {
con.addRequestProperty("Authorization", "Bearer " + token+"; charset=utf-8" );
}
if (objects[CONNECTION_METHOD].toString().equals("POST")) {
con.addRequestProperty("Content-Type", "application/json; charset=utf-8");
con.setDoInput(true);
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
out.write(objects[OUTPUT_OBJECT].toString().getBytes("UTF-8"));
}
String str;
if (con.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
str = getRespons(con.getInputStream()) /*+ jsRespons*/;
} else {
str = getRespons(con.getErrorStream()) /*+ jsRespons*/;
}
///
Map<String, List<String>> headerFields = con.getHeaderFields();
Set<String> headerFieldsSet = headerFields.keySet();
Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();
while (hearerFieldsIter.hasNext()) {
String headerFieldKey = hearerFieldsIter.next();
List<String> headerFieldValue = headerFields.get(headerFieldKey);
StringBuilder sb = new StringBuilder();
for (String value : headerFieldValue) {
sb.append(value);
sb.append("");
}
System.out.println(headerFieldKey + "=" + sb.toString() );
}
///
JSONObject jsResponse = new JSONObject(str);
jsResponse.put("resMsg", con.getResponseMessage());
jsResponse.put("response", con.getResponseCode());
return jsResponse.toString();
} catch (Exception e) {
return e.toString();
}
}
谢谢。
您从配置中调用了错误的部分。
services.Configure<ConnectionConfig>(Configuration.GetSection("ConnectionStrings"));
您还需要更新模型
public class ConnectionConfig {
public string MVCCoreAppDB { get; set; }
}
也就是说,我建议您更改设计以预先填充模型并将其注册到服务集合以进行注入
在启动中:
services.AddScoped<DataAccess>(_ =>
new DataAccess(Configuration.GetConnectionString("MVCCoreAppDB"))
);
并将数据访问显式注入控制器
private readonly DataAccess data;
public PropertyController(DataAccess data) {
this.data = data;
}
public IActionResult Index() {
var test = data.LoadData();
return View();
}
引用显式依赖原则
解决方案
Accept: application/json
根据Laravel 文档将标头添加到您的请求中。
con.addRequestProperty("Accept", "application/json");
推荐阅读
- jquery - Jquery在几个元素上的相同功能
- angular - 如何将 NGXS 与路由解析器一起使用?
- django - Django Web 应用在 Azure App Service Linux 上成功部署,但只能看到 Azure 默认页面
- mysql - 尝试在 Kubernetes 中创建两个具有相同卷的 MySQL pod 以实现高可用性
- android - 为什么单个片段在一个活动中占据整个屏幕,而其他视图使用约束布局?
- swift - UITableView 在模拟器中运行良好,但在我的物理设备中运行良好
- php - 如何在 php 中使用 curl 在标头中添加 google 身份验证
- mysql - 登录节点js api后无法返回响应
- python - 如何防止网络爬虫的 301 重定向
- c++ - Nemiver 找不到文件 /build/glibc-LK5gWL/glibc-2.23/stdlib/random.c
我尝试在标头中的应用程序中将令牌发送到服务器端。但是当我尝试发送令牌时出现此错误
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
我的服务器端是 laravel。
即使我尝试使用utf-8
但
问题描述
我尝试在标头中的应用程序中将令牌发送到服务器端。但是当我尝试发送令牌时出现此错误
org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
我的服务器端是 laravel。
即使我尝试使用utf-8
但它没有用
con.addRequestProperty("Authorization", "Bearer " + token+"; charset=utf-8" );
这是我连接到服务器时的代码
@Override
protected Object doInBackground(Object[] objects) {
try {
URL url = new URL(objects[URL_ADDRESS].toString());
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod(objects[CONNECTION_METHOD].toString());
if (token != null) {
con.addRequestProperty("Authorization", "Bearer " + token+"; charset=utf-8" );
}
if (objects[CONNECTION_METHOD].toString().equals("POST")) {
con.addRequestProperty("Content-Type", "application/json; charset=utf-8");
con.setDoInput(true);
con.setDoOutput(true);
OutputStream out = con.getOutputStream();
out.write(objects[OUTPUT_OBJECT].toString().getBytes("UTF-8"));
}
String str;
if (con.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
str = getRespons(con.getInputStream()) /*+ jsRespons*/;
} else {
str = getRespons(con.getErrorStream()) /*+ jsRespons*/;
}
///
Map<String, List<String>> headerFields = con.getHeaderFields();
Set<String> headerFieldsSet = headerFields.keySet();
Iterator<String> hearerFieldsIter = headerFieldsSet.iterator();
while (hearerFieldsIter.hasNext()) {
String headerFieldKey = hearerFieldsIter.next();
List<String> headerFieldValue = headerFields.get(headerFieldKey);
StringBuilder sb = new StringBuilder();
for (String value : headerFieldValue) {
sb.append(value);
sb.append("");
}
System.out.println(headerFieldKey + "=" + sb.toString() );
}
///
JSONObject jsResponse = new JSONObject(str);
jsResponse.put("resMsg", con.getResponseMessage());
jsResponse.put("response", con.getResponseCode());
return jsResponse.toString();
} catch (Exception e) {
return e.toString();
}
}
谢谢。
您从配置中调用了错误的部分。
services.Configure<ConnectionConfig>(Configuration.GetSection("ConnectionStrings"));
您还需要更新模型
public class ConnectionConfig {
public string MVCCoreAppDB { get; set; }
}
也就是说,我建议您更改设计以预先填充模型并将其注册到服务集合以进行注入
在启动中:
services.AddScoped<DataAccess>(_ =>
new DataAccess(Configuration.GetConnectionString("MVCCoreAppDB"))
);
并将数据访问显式注入控制器
private readonly DataAccess data;
public PropertyController(DataAccess data) {
this.data = data;
}
public IActionResult Index() {
var test = data.LoadData();
return View();
}
引用显式依赖原则
Accept: application/json
根据Laravel 文档将标头添加到您的请求中。
con.addRequestProperty("Accept", "application/json");