我正在从我的 android 客户端应用程序向 HTML 服务器发送一个 POST 请求,并且 HTML 页面返回一个 JSON 对象作为响应,但内容类型 =“text/html”。
在请求中,我传递了 2 个参数: 1. 图像文件 2. 选项值 一旦选择了“选项”,就会发送请求。而且,我期待一个包含“JSON 数组”的“JSON 对象”作为响应。
因此,当我请求接收 JSON 对象(如果我使用 .getAsJSONObject)时,我收到了一个解析错误 - “org.json.JSONException:Java.lang.String 类型的值 DOCTYPE 无法转换为JSON对象”。
如果我使用 .getAsString 以接收文本形式的响应,我将获得网页的相应“DOCTYPE”HTML 代码。服务器页面的URL为:http: //52.66.220.119 :8088/
在此,选择一个面部图像文件并从下拉菜单中选择“WoWCrown”选项并点击“TRY”按钮。您将获得 HTML 上的 JSON 对象。
我已经使用过 .getAsJSONObject() ;.getAsString() ; getAsJSONArray() 和其他一些快速 Android 网络库的获取方法,但我无法从服务器获取 JSON 对象作为响应。在其中一些中,我得到了解析错误,而在休息时,我得到了如上所述的错误。
// Inside OnItemSelectedListener()
JSONObject postObject = new JSONObject( );
RequestBody fbody = null;
File file = null;
try {
file = new File(imageUri.getPath());
Log.d( "hello file", file +"" );
fbody = RequestBody.create( MediaType.parse("image/*"), file);
Log.d( "hello fbody", fbody +"" );
postObject.put( "face", fbody);
Log.d("LIST POSITION", selectedIdSpinnerArrayList.get(position-1) );
Log.d("LIST NAME", spinnerArrayList.get( position ) );
postObject.put( "mylist", selectedIdSpinnerArrayList.get(position-1) );
} catch (JSONException e) {
e.printStackTrace();
}
AndroidNetworking.post( URL )
.addJSONObjectBody( postObject )
.setPriority( Priority.HIGH )
.setTag( "hello AN" )
.build()
.getAsJSONObject(new JSONObjectRequestListener() {
@Override
public void onResponse (JSONObject response){
Log.e("hello", response.toString());
//if (response != null)
{
Gson gson = new Gson();
Type type = new TypeToken<JSONObject>() {
}.getType();
JSONObject result = gson.fromJson(response.toString(), type);
Log.d( "hello", result.toString() );
}
}
@Override
public void onError (ANError anError){
Log.d("hello GSON", anError.getLocalizedMessage() + anError.getCause());
}
});
预期响应是:- 来自客户端 android 应用程序的 POST 请求的 JSON 对象。
错误:-
关于使用 .getAsJSONObject() 方法 ---> org.json.JSONException: Value DOCTYPE of type java.lang.String cannot be convert to JSONObject
关于使用 .getAsString() 方法 ---> 页面的准确 HTML 代码。