java - android - 从字符串创建json对象来处理网络错误

标签 java android json

我需要创建 json 对象来处理网络调用错误

我解析错误的方式

{error:errorMessage}

我的尝试:

 String error = "{\"error\":\""+ e.getMessage()+"\"}";

但是当我转换 jsonObject = new JSONObject(stringBuilder.toString());

抛出异常

我的代码

public class HttpBackground implements Runnable {
        HttpDefs client;
        String error = null;
        JSONObject jsonObject;

        public HttpBackground(HttpDefs client) {
            this.client = client;
        }

        @Override
        public void run() {
            final StringBuilder stringBuilder = new StringBuilder();
            String line = null;
            BufferedReader reader = null;

            try {

                HttpURLConnection connection = (HttpURLConnection) client._url.openConnection();
                connection.setRequestMethod(client.Request_type);
                connection.setReadTimeout(AppStaticMembers.WAIT_RESPONSE_TIMEOUT);

                OutputStream outputStream = connection.getOutputStream();
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                writer.write(getQuery(client.params));
                writer.flush();
                writer.close();
                outputStream.close();
                connection.connect();
                if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                } else {
                    reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                }
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }


            } catch (SocketTimeoutException e) {
                error = "{\"error\":\""+ e.getMessage()+"\"}";

            } catch (Exception e) {
                e.printStackTrace();
                error = "{\"error\":\""+ e.getMessage()+"\"}";
            } finally {
                if (reader != null) {

                    try {
                        reader.close();


                        stringBuilder.append(error);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            try {
                jsonObject = new JSONObject(stringBuilder.toString());
            } catch (JSONException e) {
                e.printStackTrace();
            }


            httpUI httpUI = new httpUI(jsonObject, this.client);
            handler.post(httpUI);
        }

    }

最佳答案

你有

String error = "{\"error\":\""+ e.getMessage()+"\"}";

您应该转义/删除错误消息中的任何双引号,例如

String escapedError = e.getMessage().replace("\"", "");
String error = "{\"error\":\""+ escapedError +"\"}";

关于java - android - 从字符串创建json对象来处理网络错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32144698/

相关文章:

java - 堆栈溢出错误

android - 为什么不同版本的 Android 会以不同的方式重新排序我的 JSON?

ios - iPhone 设备中的 Json 解析错误,但在模拟器中正确

Android - 将日期的秒数设置为零

javascript - Angular 2 发送空帖子

java - 如何仅在带有 ImageView 小部件的 xml 动画期间使用不同的可绘制对象 (android)

java - 无法连接到 NetBeans 7.0.1 中的 Derby 数据库

java - 带有自定义类加载器的 Jmockit : System class loader does not support adding JAR file to system class path during the live phase

java - 从 java 代码创建 PDF 文件 [不使用 iText]

android - Twitter4j TwitterStream 停止/关闭