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 に答える