在我的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
我正在尝试使用split来匹配此表单: N |类别,纬度,经度,距离|类别,纬度,经度,距离...
当我使用“|”时它没有分裂任何东西。
这是我的代码:
@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
有任何想法,请问如何使用“,”和“|”?
答案 0 :(得分: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]);
}
}
}
}