2

在我的 doInBackground(String... arg0) 方法中,我得到一个包含以下内容的结果变量:

4|Litter Bin,-37.8141472000103,144.963691391683,17.354417684886|Litter Bin,-37.8141472763581,144.963685395193,17.179052776008|Litter Bin,-37.8139653160326,144.963765797949,13.429259628312|Litter Bin,-37.8139469233985,144.963755935562,13.402390334431

我正在尝试使用拆分来匹配这种形式:N|category,latitude,longitude,distance|category,latitude,longitude,distance...

当我使用“|” 它不会分裂任何东西。

这是我的代码:

@Override
protected Integer doInBackground(String... arg0) {


    String result = "";
    int responseCode = 0;

    int executeCount = 0;
    HttpResponse response;

    StringBuilder sb = new StringBuilder();
    String line;
    try 
    {
        HttpClient client = new DefaultHttpClient();
        HttpGet httppost = new HttpGet("http://XXX/ccvo/mel-asset-data/query.php?lon="+ arg0[0].toString() + "&lat="+ arg0[1].toString() +"&within=" + arg0[2].toString()  + "&keyword="+ arg0[3].toString().replace(" ", "%20"));


        Log.v("Results", "from web: " + arg0[0]);
        Log.v("Results", "from web: " + arg0[1]);
        Log.v("Results", "from web: " + arg0[2]);
        Log.v("Results", "from web: " + arg0[3]);

            do
            {
                progressDialog.setMessage("Passing paratmeters.. ("+(executeCount+1)+"/5)");
                // Execute HTTP Post Request
                executeCount++;
                response = client.execute(httppost);
                responseCode = response.getStatusLine().getStatusCode();                        

            } while (executeCount < 5 && responseCode == 408);

            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            while ((line = rd.readLine()) != null)
            {
                result = line.trim();
                sb.append(line);

            }
    }catch (Exception e2) {
        responseCode = 408;
        e2.printStackTrace();
        }
    rst = result.toString();
    if(rst != null && rst.length() > 0)
    {
      strArr = rst.split(",");
      for(int i=0;i<strArr.length;i++)
      {
        Log.d("Results", "Array split 1: " + strArr[i]);
      }
    }


    return responseCode;
}

输出:

10-13 16:26:30.292: D/Results(10500): Array split 1: -37.8141472000103
10-13 16:26:30.292: D/Results(10500): Array split 1: 144.963691391683
10-13 16:26:30.312: D/Results(10500): Array split 1: 17.354417684886|Litter Bin
10-13 16:26:30.312: D/Results(10500): Array split 1: -37.8141472763581
10-13 16:26:30.322: D/Results(10500): Array split 1: 144.963685395193
10-13 16:26:30.322: D/Results(10500): Array split 1: 17.179052776008|Litter Bin
10-13 16:26:30.322: D/Results(10500): Array split 1: -37.8139653160326
10-13 16:26:30.362: D/Results(10500): Array split 1: 144.963765797949
10-13 16:26:30.362: D/Results(10500): Array split 1: 13.429259628312|Litter Bin
10-13 16:26:30.362: D/Results(10500): Array split 1: -37.8139469233985
10-13 16:26:30.362: D/Results(10500): Array split 1: 144.963755935562
10-13 16:26:30.362: D/Results(10500): Array split 1: 13.402390334431

任何想法请如何使用“,”和“|”?

4

1 回答 1

3

您在获取字符串时编写了错误的逻辑。只是放在rst = result.toString()外面一会儿。

BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null)
{
  result = line.trim();
  sb.append(line);                   
}
rst = result.toString();

然后检查字符串是否不为空

if(rst != null && rst.length > 0)
{
  strArr = rst.split(",");
  for(int i=0;i<strArr.length;i++)
  {
    Log.d("Results", "Array split 1: " + strArr[i]);
  }
}

拆分为“|”

if(rst != null && rst.length > 0)
{
  strArr1 = rst.split("|");
  for(int i=0;i<strArr1.length;i++)
  {
    if(strArr1[i] != null && strArr1[i].length >0 && strArr1[i].contains(","))
    {
      String strArr2 = strArr1[i].split(",");
      for(int j=0; j<strArr2.length ;j++)
      {
        Log.d("Results", "Array split "+i+": " + strArr2[j]);
      }
    }
  }
}
于 2012-10-13T05:08:57.970 回答