【问题标题】:How to remove 404 file not found error when trying to recieve json array from url? [closed]尝试从 url 接收 json 数组时如何删除 404 file not found 错误? [关闭]
【发布时间】:2018-12-20 11:44:43
【问题描述】:

我正在尝试获取 JSON 响应,它采用数组格式。这个 JSON 响应我想转换成 Java 数组。

这是数组:

[{"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();
}

【问题讨论】:

  • 正是您在这里面临的问题。有什么例外吗?
  • 我想收到以上所有数据,然后我会将名称放入数组中,暂时不使用 id 但仍然派上用场,我收到 404 错误

标签: java arrays json


【解决方案1】:

从该 URL 读取的问题是显然需要“User-Agent”请求标头。我设法用下面的代码阅读了你的 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;
    }
}

【讨论】:

  • 我正在使用 java ide 进行核心和桌面应用程序,java reader 没有得到解决,如何在简单的 ide 中工作
  • 是 urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0");给出了响应数据
【解决方案2】:
You can user JSONArray
e.g- 

JSONArray jsonArray = new JSONArray(result.toString());
and afterwords you can parse that json arrray object using 

ArrayList<String> listdata = new ArrayList<String>();
for(int n = 0; n < jsonArray.length(); n++)
{
    JSONObject object = jsonArray.getJSONObject(n);
    listdata.add(object.optString("n")); 
}

return listdata;

【讨论】:

    猜你喜欢
    • 2017-10-19
    • 2018-10-27
    • 2012-09-27
    • 1970-01-01
    • 2016-08-21
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多