有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从HTML服务器获取JSON对象作为响应

我正在从安卓客户端应用程序向HTML服务器发送POST请求,HTML页面返回一个JSON对象作为响应,但内容类型为=“text/HTML”

在请求中,我传递了两个参数:1。图像文件2。期权价值 选择“选项”后,将立即发送请求。而且,我希望有一个“JSON对象”,包括一个“JSON数组”作为响应

因此,当我请求接收JSON对象时(如果我使用的是.getAsJSONObject),我得到了一个解析错误——“org.JSON.JSONException:Value DOCTYPE类型java.lang.String无法转换为JSONObject”

如果我正在使用。getAsString以便以文本形式接收响应,我将获得网页的相应“DOCTYPE”HTML代码。 服务器页面的URL为:http://52.66.220.119:8088/

在这种情况下,选择一个面部图像文件,从下拉菜单中选择“WoWCrown”选项,然后点击“TRY”按钮。您将在HTML上获得JSON对象

我已经用过了。getAsJSONObject()。getAsString();getAsJSONArray()和其他一些快速Android网络库的get方法,但我无法从服务器获取JSON对象作为响应。在其中一些示例中,我得到了解析错误,而在rest中,我得到了如上所述的错误

// 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());
                            }
                        });

预期的答复是:- 客户端安卓应用程序发出POST请求时的JSON对象

错误:-

在使用上。getAsJSONObject()方法-->; 组织。json。JSONException:java类型的值DOCTYPE。无法将lang.String转换为JSONObject

在使用上。getAsString()方法-->; 页面的确切HTML代码

screenshot of the JSON Object is attached as link


共 (0) 个答案