Reputation:
I'm trying to print The destination addresses & their distances from the origin in the textView. However, I keep getting error or just show the last value. I don't want the text view get updated, I want the new value to be printed under the new one Here is my code
public class MainActivity extends Activity {
private static final String TAG_ROWS = "rows";
private static final String TAG_ELEMENTS = "elements";
private static final String TAG_DISTANCE = "distance";
private static final String TAG_VALUE = "value";
private static final String TAG_ADDRESS = "destination_addresses";
String Addresses[]= {"2906+west+Broadway+Vancouver+BC","4750+Kingsway+Burnaby+BC","2633+Sweden+Way+110+Richmond","943+Marine+Dr+North+Vancouver","4567+Lougheed+Hwy+Burnaby"};
String data;
HttpClient client;
double minDistance=0;
static JSONObject jObj = null;
String destination_addresses;
JSONArray rows;
String destination;
String distanceStr;
String[] value_destination;
String value;
final static String URL= "http://maps.googleapis.com/maps/api/distancematrix/json?";
TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
result = (TextView) findViewById(R.id.text1);
result.setText("Distace from the location" + destination + " is :" + distanceStr );
new TestGoogleMaps().execute("");
}
public class TestGoogleMaps extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
try {
ClosestObject();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
finally {
}
return null;
}
@Override
protected void onPostExecute(String resuls) {
// TODO Auto-generated method stub
super.onPostExecute(resuls);
}
}
public void ClosestObject() throws JSONException, ClientProtocolException, IOException {
// Creating JSON Parser instance
StringBuilder url = new StringBuilder(URL);
client=new DefaultHttpClient();
for (int index=0; index<Addresses.length; index++){
String str_parameters = "origins="+ URLEncoder.encode("1359+Richards+Street+Vancouver+BC","UTF-8")+"&destinations="+ URLEncoder.encode(Addresses[index],"UTF-8")+"&mode=driving&language="+ URLEncoder.encode("en-FR","UTF-8")+"&sensor=false";
System.out.println("URL URl :: "+url+str_parameters);
HttpGet get = new HttpGet(url+str_parameters);
get.setHeader("Accept", "application/json");
get.setHeader("Content-type", "application/json");
HttpResponse r = client.execute(get);
HttpEntity en = r.getEntity();
data = EntityUtils.toString(en);
System.out.println("ClosestObject Response :: "+data);
try {
jObj = new JSONObject(data);
destination = jObj.getString("destination_addresses");
// printing the destination and checking wheather parsed correctly
Log.v("Destination", destination);
JSONArray jarRow = jObj.getJSONArray("rows");
for(int i=0;i<jarRow.length(); i++){
// creating an object first
JSONObject ElementsObj = jarRow.getJSONObject(i);
// and getting the array out of the object
JSONArray jarElements = ElementsObj.getJSONArray("elements");
for(int j=0; j<jarElements.length(); j++){
JSONObject distanceObj = jarElements.getJSONObject(j).getJSONObject("distance");
distanceStr = distanceObj.getString("value");
Log.v("finally getting distance : ", distanceStr);
} }
} catch (JSONException e) {
e.printStackTrace();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
How can I print all the members on my TextView?
Upvotes: 0
Views: 2712
Reputation: 33495
I want the new value to be printed under the new one
So if you want to print your all destinations into TextView
most efficient way will be to use for example StringBuffer
to create whole String that will be finally assigned to your TextView.
I recommend you change your return type of ClosestObject method to StringBuffer(or Builder) and in your loop append data to it. Also change third parameter of AsyncTask to StringBuffer.
Pseudo-code:
@Override
protected StringBuffer doInBackround() {
...
StringBuffer buff = ClosestObject();
return buff;
}
In your ClosestObject method:
StringBuffer buff = new StringBuffer();
for (int i = 0; i < arr.length(); i++) {
// getting values from JSON
buff.append(value).append("\n"); // buff.append(value1 + "," + value2 + "\n")
}
...
return buff;
and finally update your TextView
from onPostExecute() method that already runs on UI Thread and allows updates.
yourTextView.setText(result.toString());
Don't forget that by naming conventions in Java, method's signature should start with lowercase letter and not with uppercase.
Upvotes: 1
Reputation: 12656
Try this:
MainActivity.this.runOnUiThread(new Runnable() {
public void run() {
result.setText(result.getText().toString() + "\n" + distanceStr);
}
});
You must update the TextView
on the UI Thread as shown because the JSON response is received in a different Thread from your AsyncTask
, otherwise you will receive a CalledFromTheWrongThreadException
.
Upvotes: 0