首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用JSONObject在Android中保存包含<br />标记的数组中的/>?

如何使用JSONObject在Android中保存包含<br />标记的数组中的/>?
EN

Stack Overflow用户
提问于 2013-12-09 08:52:28
回答 2查看 450关注 0票数 0

我是android.Can的新手,请帮助我如何去除

在JSON中,然后将其存储在一个数组中,然后使用java.Here在循环中获取值是JSON:我正在编写以下代码:

代码语言:javascript
复制
String jsonArr[];
     private void getRequiredProducts(String parameter)
         {   Toast.makeText(context,"getRequiredProducts",Toast.LENGTH_SHORT).show();
             try
             {

            responseJson = new JSONObject();
            responseJson = ServerRequest.sendRequest(url,parameter);
            Log.i(product_Tag,responseJson.toString());
             jsonArr = responseJson.toString().split("<br />");
            Log.i("ProductsArray:","values:" + jsonArr);
             }
             catch (JSONException e) {
                    errorMessage = getString(R.string.ExceptionGeneral);
                    ExceptionHandler.logException(e);
                    e.printStackTrace();
                }


    }

但是每次我在控制台中遇到异常时:

12-10 00:35:52.330: W/System.err(25377):org.json.JSONException: java.lang.Integer类型的值53不能转换为JSONObject 12-10 00:35:52.330: W/System.err(25377):at org.json.JSON.typeMismatch(JSON.java:111) 12-10 00:35:52.330: W/System.err(25377):at org.json.JSONObject.(JSONObject.java:158) 12-1000:35:52.330: W/System.err(25377):at org.json.JSONObject.(JSONObject.java:171) 12-10 00:35:52.330: W/System.err(25377):at ServerRequest.java添加的代码

代码语言:javascript
复制
public class ServerRequest {

    public static JSONObject sendRequest(String url, String parameter)
            throws UnknownHostException, SocketException, IOException,
            JSONException {
        JSONObject responseJson = null;
        HttpClient httpclient = SessionManager.getHttpClient();
        HttpPost httppost = new HttpPost(url);
      httppost.addHeader("content-type", "application/x-www-form-urlencoded");
        if (!parameter.equals("null")) {
            httppost.setEntity(new StringEntity(parameter));

        }
        Log.i("URL>>>>>>>>>>>>>>>>",url);
        Log.i("PARAMETERS>>>>>>>>>>>>>>>>", parameter);
        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        StringBuilder responseStr = AndroidUtil.inputStreamToString(response
                .getEntity().getContent());
        Log.i("responseJson http:", "" + response);
        Log.i("responseString", "" + responseStr);
        responseJson = new JSONObject(responseStr.toString());
        Log.i("responseJson", "" + responseJson);
        return responseJson;
    }
}

这是我从服务获得的JSON :和一行Log.i("responseJson“、”+responseJson“)的日志;

12-1001:17:54.565: I/responseString(2563):53,"ACCESS 08","Galaxy问起接近键标签(Grey) YXO 0020",21,NULL,3.03,4.84,"ACCESS_08.JPG","Galaxy“,NULL,”GalaxYX0-0020“,”PAN206“,0.01,0,0,0.01,0.01,"Y","7010",”是“,6.75,空,”活动“54,“电池06”、"3.6v锂1/2 AA电池“、21、空、2.5、4、"BATTERY_06.JPG”、"1/2 AA电池“、空、”夏娃ER14250“、”BAT034“、0.01、0、0.01、0.01、"Y”、"7010“、”是“、7.5、零、空、”活动“55、”电池07“、"AA电池”、21、空、0.4、0.64、"BATTERY_07.JPG“、"AA电池”、NULL、"PROCELL MN1500“、”BAT019“、0.01,0、0、0.01、"Y”、"7010“、”是“、2.5、空、”活动“59、”控制26“、"Scantronic 8400 8声道数字”、21、NULL、31.79、54.4、"CONTROL_26.JPG“、"Scantronic 8400 8声道数字”、NULL、“8400 21 21”,“8400 21 21”、0.1、0、0、1、0.1、"Y“、"7010”、“是”、65.45、NULL、"Scantronic 8400 8声道数字“、NULL、NULL、"active” 60、"FIRE 07“、"KAC SR 2072-SR表面呼叫点”、“21、NULL、6、9.6、"FIRE_07.JPG”、"KAC SR 2072-SR表面呼叫点“、"KAC (4003)”、“CAL002”、0.033、0、0、0.5、0.01、"Y“、"7010”、“是”、48.55、空、“活动”63、"MISC 06“、"TKC Handy Relay 12v DC”、21、NULL,7.4,11.85,"MISC_06.JPG","TKC HLR手持小继电器12v DC",NULL,"REL000",“REL000”,0.01,0,0,0.01,0.01,"Y","7010",“是”,14.208,NULL,"TKC HLR手持小继电器12v DC",NULL,NULL,"active“ 64,"MISC 07","TKC非常方便的12v DC",21,NULL,8.27 13.23,"MISC_07.JPG","TKC HLR手持小继电器12v DC",NULL,"REL002",“REL002”,0.01,0,0,0.01,0.01,"Y","7010",“是”,15.8784,15.8784,NULL,"TKC非常方便的小继电器12vDC“,空,空,”活动“67,”电源02","ACT 1313 12v斯派克抑制剂“,21,NULL,7.5,12,"POWER_02.JPG","ACT 1313 12v Spike抑制器”,“空”,"SPI001",“SPI001”,0.01,0,0,0.5,0.01,"Y","7010",“是”,25,NULL,"ACT 1313 12v斯派克抑制器“,NULL,NULL,"active” 68、“信号07”、“对3G STU (独立)”、21、NULL、135、216、"No_Image.PNG“、”对3G STU (独立)“、NULL、”对38232 3G“、”COM012“、0.01、0、0、1、0.01、"Y”、"7010“、”是“、259.2、NULL、”对3G STU (独立)“、NULL、NULL、"active” 69、“10号信号”、“纺织品总理委员会300”、21、空、27.69、46.96、"SIGNAL_10.JPG“、”德州总理Com 300“、”COM013“、0.01、0、0、1、0.01、"Y”、"7010“、”是“、56.5、空、”德州总理Com 300“、空、”活动“ 70,“烟雾09",”阿波罗S65 24v光学烟雾探测器D206",21,NULL,17.65,28.24,"SMOKE_09.JPG",“阿波罗S65 24v光学烟雾探测器D206”,NULL,"SMO007","SMO007",0.033,0,0,0,1,0.01,"Y","7010",“是”,60,NULL,“阿波罗S65 24v光学烟雾探测器D206”,NULL,NULL,"active“ 71,“烟雾11",”阿波罗XP95基地“,21,空,1.8,2.88,"SMOKE_11.JPG",”阿波罗XP95基地“,空,"SMO015",”SMO015“,0.01,0,0,0.01,"Y","7010",”是“,4.37,空,”阿波罗XP95基地“,空,空,”活动“ 72、“烟雾15”、“烟雾罐(检测器测试仪)”、21、NULL、7.5、12、"SMOKE_15.JPG“、"Can (检测器测试仪)”、“NULL”、"SOLO 83001“、”CAN001“、0.01、0、0、0.01、0.01、Y、7010、”是“、14.44、NULL、"Can of烟雾(检测器测试仪)”、NULL、NULL、"active“ 80,“电源24","CR123A 3v立陶宛电池”,21,空,1.85,3.92,"No_Image.PNG","CR123A 3v立陶宛电池“,NULL,"VISONIC 0-9913-N",”BAT042“,0.01,0,0,0.01,"Y","7010",”是“,5.64,空,"CR123A 3v锂电池”,空,空,“活动” 84,"INTLESS0027","Powermax 124无线双按钮便携式发送器“,18,NULL,25,51.64,"INTLESS0027.JPG",”带有双按钮操作的无线双按钮便携式发送器,以防止意外遇险信号“,NULL," MCT-124 ","RAD198",0.01,0,0,0,0.25,0.01,"Y","7010",”是“,62.14,空,"Powermax MCT-124无线双按钮便携发送器”,空,空,“活动”。 85、"CABLE0027“、"CAT5导管级电缆305 M盒”、11、NULL、35.95、120、"Cable0027.JPG“、"CAT5导管级电缆305 M盒”、NULL、"COMMTECH B0193AXNCPE“、”CAB175“、0.01、0、0、4、0.01、"Y”、"7010“、”NO“、144.38、NULL、CAT5导管级电缆305m、空、”活动“86、"SECLIGHT0073”、"3 way 20amp 240 NULL“、17、NULL2,3.52,"SECLIGHT0073.JPG","3 way 20 0.0 240 NULL连接框“,NULL,”单击WA074","JBS014",0.0

EN

回答 2

Stack Overflow用户

发布于 2013-12-09 12:51:38

我不需要使用BufferReader,我只是从我的ServerRequest类返回字符串,而不是返回调用函数和应用循环的JSON.After。

代码语言:javascript
复制
public class ServerRequest1 {

    public static String sendRequest(String url, String parameter)
            throws UnknownHostException, SocketException, IOException,
            JSONException {
        JSONObject responseJson = null;
        HttpClient httpclient = SessionManager.getHttpClient();
        HttpPost httppost = new HttpPost(url);
        httppost.addHeader("content-type", "application/x-www-form-urlencoded");
        if (!parameter.equals("null")) {
            httppost.setEntity(new StringEntity(parameter));

        }

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
        StringBuilder responseStr = AndroidUtil.inputStreamToString(response
                .getEntity().getContent());
          return responseStr.toString();
    }
}

另一个java类中的

代码语言:javascript
复制
@Override
    protected Void doInBackground(String...params) {

    getRequiredProducts(parameter);
    return null;

    }
     private void getRequiredProducts(String parameter)
     { 
         try
         {
             String getRequiredProducts_String  = ServerRequest1.sendRequest(url,parameter);
             String[] getRequiredProducts_Str_Array = getRequiredProducts_String.split("<br />");

             for(int i=0;i<getRequiredProducts_Str_Array.length;i++)
             {
                 Log.i("getRequiredProducts_Str_Array :",getRequiredProducts_Str_Array[i]);
             }
          }
         catch (JSONException e) {
                errorMessage = getString(R.string.ExceptionGeneral);
                ExceptionHandler.logException(e);
                e.printStackTrace();
            }
}
票数 1
EN

Stack Overflow用户

发布于 2013-12-09 09:38:06

如下所示解析CSV文件数据:

代码语言:javascript
复制
 BufferedReader reader = new BufferedReader(new InputStreamReader(response));
    try {
        String line;
        while ((line = reader.readLine()) != null) {
             String[] RowData = line.split(",");
             date = RowData[0];
             value = RowData[1];
            // do something with "data" and "value"
        }
    }
    catch (IOException ex) {
        // handle exception
    }
    finally {
        try {
            is.close();
        }
        catch (IOException e) {
            // handle exception
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20466588

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档