Reputation: 149
In my doInBackground(String... arg0) method I get a result variable containing this:
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
I'm trying to use split to match this form: N|category,latitude,longitude,distance|category,latitude,longitude,distance...
when I use "|" it does not split anything.
here is my code:
@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;
}
Output:
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
Any thoughts please how to use "," and "|"?
Upvotes: 2
Views: 222
Reputation: 9580
You are writing wrong logic with getting string.
just put the rst = result.toString()
outside the while.
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null)
{
result = line.trim();
sb.append(line);
}
rst = result.toString();
and then check whether the string is not empty with
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]);
}
}
spliting for "|"
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]);
}
}
}
}
Upvotes: 3