【问题标题】:JSON parsing in Android Studio 1.5.1Android Studio 1.5.1 中的 JSON 解析
【发布时间】:2016-04-03 12:34:55
【问题描述】:

我在 Eclipse ADT 上进行了 JSON 解析,它运行良好。但是当在 Android Studio 上使用相同的代码时,它会在 DefaultHttpClient 和该块中的所有其他单词中显示错误。

这是我的 JSON 解析器类

JSONParser.java

public class JSONParser {

    JSONArray cArray;
    JSONObject jsonObj;
    InputStream is = null;
    String jsonStr = "";
    String urlString;
    Context context;
    String id;
    String name, email, mobile;

    Contacts contacts;
    List < Contacts > contactList;

    public JSONParser() {
        // TODO Auto-generated constructor stub
    }

    public JSONParser(String url) {
        this.urlString = url;
    }

    public List < Contacts > getString() {

        contactList = new ArrayList < Contacts > ();

        try {
            // **Error in this block**

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(urlString);

            HttpResponse httpResponse = httpClient.execute(httpPost);

            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            Log.e("NULL DATA", "cant fetch " + e.toString());
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            jsonStr = sb.toString();


        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }


        if (jsonStr != null) {
            try {

                cArray = new JSONArray(jsonStr);

                JSONObject jsonObj = null;

                // looping through All Contacts
                for (int i = 0; i < cArray.length(); i++) {
                    jsonObj = cArray.getJSONObject(i);

                    id = jsonObj.getString("foodno");
                    name = jsonObj.getString("foodname");
                    email = jsonObj.getString("foodtype");

                    /* JSONObject phoneNo = jsonObj.getJSONObject("phone");
                     mobile = phoneNo.getString("mobile");*/

                    contacts = new Contacts(id, name, email);


                    // adding contact to contact list
                    contactList.add(contacts);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (NullPointerException e) {
                e.printStackTrace();
            }

        } else {
            Log.e("JSONParser", "Couldn't get any data from the url");
        }


        return contactList;

    }

}

谁能建议如何连接以获取我可以传递给 BufferedReader 的 json 字符串?

【问题讨论】:

  • 你说 okHttp 对你来说是最好的。但是为此我必须为每台新 PC 下载 jar 文件,或者可以在不下载 jar 文件的情况下完成?
  • 我知道你指的是什么“每台新电脑”。在 Android Studio 中,在build.gradle 文件中添加一行,OkHttp3 就会下载到开发机器上。 OkHttp3 的代码将被合并到您的项目中并打包到您的 APK 中,就像您已经在使用的任何其他库一样。
  • 感谢您的帮助:)

标签: android json eclipse


【解决方案1】:

我强烈建议您使用库来完成这项工作。您可以使用GSON lib(由谷歌开发)或Jackson lib。对于请求,您可以使用Retrofit lib(由 Square 开发)。

【讨论】:

    【解决方案2】:

    build.gradle

    中添加Gson依赖
    compile 'com.google.code.gson:gson:2.4'
    

    然后为 json 响应创建 POJO 类。

    为此,只需将您的 json 响应字符串复制到下面的链接中。

    json to pojo converter

    以右侧形式提供您的类名和包名并下载该文件。

    现在回到我们的应用代码。

    Demo demo = new Gson().fromJson("JSON STRING",Demo.class);
    

    Demo是我们从json to pojo converter创建的POJO类

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 2020-08-04
      • 1970-01-01
      • 1970-01-01
      • 2021-10-30
      • 2015-10-19
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      相关资源
      最近更新 更多