org.json.jsonexception:无法将java.lang.string类型的http值转换为jsonobject

o2gm4chl  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(323)

我对json解析非常陌生,而且还使用api,所以我不知道为什么会发生这种情况。如果您能帮助解决这个问题,我们将不胜感激。
现在来谈谈这个问题。
在创建我的springboot应用程序时,我遇到了一个jsonexception,我就是搞不清楚,而我正试图显示openweathermap中的api数据。
org.json.jsonexception:无法将java.lang.string类型的http值转换为jsonobject
好像是在这条线上发生的

String city = getCityById(id);

这是完整的java文件(我的api密钥经过了编辑)

package Homework4_API.Homework4.controllers;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;

@RestController
public class MainController {

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ModelAndView get(@RequestParam("id") String id) {
        ModelAndView mv = new ModelAndView ("redirect:/");
        String city = getCityById(id);
        try{
            JSONObject json = new JSONObject(city);
            mv.addObject("name", json.getString("name"));
            mv.addObject("temperature", json.getJSONObject("main").get("temp").toString());
            mv.addObject("feels_like", json.getJSONObject("main").get("feels_like").toString());
            mv.addObject("humidity", json.getJSONObject("main").get("humidity").toString());
            mv.addObject("weather", json.getJSONObject("weather").get("description").toString());

        }
        catch (Exception e){
            System.out.println(e.toString());
        }

        return mv;
    }

    private String getCityById(String id){
        try {
            String apiKey = "[redacted]";
            URL URLForRequest = new URL("https://api.openweathermap.org/data/2.5/weather?q=" + id + "&appid=" + apiKey);

            HttpsURLConnection connection = (HttpsURLConnection) URLForRequest.openConnection();
            connection.setRequestMethod("GET");
            if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null){
                    response.append(line);
                }
                in.close();
                return response.toString();
            }
            else {
                return "HTTP SAID NOOOOOO!!";
            }
        }
        catch (Exception e){
            return "HTTP Said No";
        }
    }
}

class City
{
    private String City;

    public City(){
        City = "DEFAULT";
    }

    public City(String c) {
        City = c;
    }

    public String getCity() {
        return City;
    }

}

html文件

<!DOCTYPE html>
<html>
<head>

    <title></title>
    <style><%@include file ="../css/style.css"%></style>

</head>

<body>

    <h1>Please Select A City</h1>
    <form method="get" action="/get/">
        <select name = "id">
            <option value ="1">Houston</option>
            <option value ="2">San Antonio</option>
            <option value ="3">Dallas</option>
            <option value ="4">Austin</option>
            <option value ="5">Fort Worth</option>
            <option value ="6">Phoenix</option>
            <option value ="7">Atlanta</option>
            <option value ="8">Chicago</option>
            <option value ="9">Los Angles</option>
            <option value ="10">New York City</option>
        </select>
        <input type="submit" value="Submit">
    </form>

    <div>
        <h2>City</h2> <h3><%=request.getParameter("name")%></h3>
        <h2>Temperature</h2> <h3><%=request.getParameter("temperature")%></h3>
        <h2>Feels Like</h2> <h3><%=request.getParameter("feels_like")%></h3>
        <h2>Humidity</h2> <h3><%=request.getParameter("humidity")%></h3>
        <h2>Weather</h2> <h3><%=request.getParameter("weather")%></h3>
    </div>

    <hr>
    <div>
        <h2>Previously Requested Cities</h2>
    </div>
    </hr>

</body>

</html>

xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.0</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>Homework4_API</groupId>
    <artifactId>Homework4</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>Homework4</name>
    <description>Spring boot for homework 4</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
        </dependency>

        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
            <scope>compile</scope>
        </dependency>

        <dependency>
            <groupId>com.vaadin.external.google</groupId>
            <artifactId>android-json</artifactId>
            <version>0.0.20131108.vaadin1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我确实有一个css文件,里面什么都没有,一个应用程序文件和主运行文件,但是我不认为他们在做任何事情来引起这个特定的问题。

jvlzgdj9

jvlzgdj91#

返回一个pojo,如果else条件被触发,则在其中放入一个异常字段并填充这个异常字段。如果一切都好的话,返回填充对象。

mjqavswn

mjqavswn2#

看看你的代码 return "HTTP SAID NOOOOOO!!"; 正在执行,这不是json,所以它抛出错误。

相关问题