Java 当尝试从url接收json数组时,如何删除404文件未找到错误?

Java 当尝试从url接收json数组时,如何删除404文件未找到错误?,java,arrays,json,Java,Arrays,Json,我正在尝试获取JSON响应,它是一种数组格式。我想在Java数组中转换这个JSON响应 这是阵列: [{"id":"1","Name":"Daniyal"}, {"id":"2","Name":"Aslam"}, {"id":"3","Name":"Kamal"}, {"id":"4","Name":"Asghar"}, {"id":"5","Name":"Jamal"}, {"id":"6","Name":"Suraj"}, {"id":"7","Name":"Mujji"}]

我正在尝试获取JSON响应,它是一种数组格式。我想在Java数组中转换这个JSON响应

这是阵列:

[{"id":"1","Name":"Daniyal"},
 {"id":"2","Name":"Aslam"},
 {"id":"3","Name":"Kamal"},
 {"id":"4","Name":"Asghar"},
 {"id":"5","Name":"Jamal"},
 {"id":"6","Name":"Suraj"},
 {"id":"7","Name":"Mujji"}]
这是代码:

try {
    URL urlForGetRequest = new URL("http://xenzet.com/ds/ds.php?");

    String readLine = null;
    HttpURLConnection conection = (HttpURLConnection) urlForGetRequest.openConnection();

    conection.setRequestMethod("GET");
    conection.setRequestProperty("userId", "a1bcdef"); // set userId its a sample here
    int responseCode = conection.getResponseCode();

    System.out.println("\nSending 'GET' request to URL : " + urlForGetRequest);
    System.out.println("Response Code : " + responseCode);
    BufferedReader in = new BufferedReader(
            new InputStreamReader(conection.getInputStream()));
    String inputLine;
    StringBuffer response = new StringBuffer();
    while ((inputLine = in.readLine()) != null) {
        response.append(inputLine);
    }
    in.close();
    //print in String
    System.out.println(response.toString());
    //Read JSON response and print
    JSONArray myResponse = new JSONArray(response.toString());
    System.out.println("result after Reading JSON Response");


} catch (Exception ex) {
    // TODO Auto-generated catch block
    ex.printStackTrace();
}
您可以使用JSONArray
e、 g-
JSONArray JSONArray=新的JSONArray(result.toString());
以及后记,您可以使用
ArrayList listdata=新的ArrayList();
for(int n=0;n
从该URL读取的问题在于显然需要“用户代理”请求头。我用下面的代码阅读了您的JSON:

import java.io.*;
import java.net.*;

public class JsonReader {

    public static void main(String[] args) throws IOException {
        JsonReader jsonReader = new JsonReader();
        jsonReader.read("http://xenzet.com/ds/ds.php?");
    }

    public void read(String url) throws IOException {
        URLConnection urlConnection = getUrlConnection(url);
        String json;
        try (InputStream inputStream = urlConnection.getInputStream()) {
            json = readContent(inputStream);
        }
        System.out.println("JSON: " + json);
    }

    private String readContent(InputStream inputStream) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            sb.append(inputLine);
        }
        return sb.toString();
    }

    private URLConnection getUrlConnection(String url) throws IOException {
        URLConnection urlConnection = new URL(url).openConnection();
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
        return urlConnection;
    }
}

你到底面临什么问题。有什么例外吗?我想接收所有上述数据,然后我会将名称放入数组中,暂时不使用id,但仍然很方便,我收到404个错误。我正在为核心和桌面应用程序使用java ide,java reader没有得到解决,如何在简单的ideyes urlConnection.setRequestProperty(“用户代理”,“Mozilla/5.0”)中工作;给出了响应数据
import java.io.*;
import java.net.*;

public class JsonReader {

    public static void main(String[] args) throws IOException {
        JsonReader jsonReader = new JsonReader();
        jsonReader.read("http://xenzet.com/ds/ds.php?");
    }

    public void read(String url) throws IOException {
        URLConnection urlConnection = getUrlConnection(url);
        String json;
        try (InputStream inputStream = urlConnection.getInputStream()) {
            json = readContent(inputStream);
        }
        System.out.println("JSON: " + json);
    }

    private String readContent(InputStream inputStream) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String inputLine;
        while ((inputLine = br.readLine()) != null) {
            sb.append(inputLine);
        }
        return sb.toString();
    }

    private URLConnection getUrlConnection(String url) throws IOException {
        URLConnection urlConnection = new URL(url).openConnection();
        urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");
        return urlConnection;
    }
}