• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps concatinating a string with a url

cathkin

Lurker
Right, I have a string in my activity class. My URL is in my httpRetriever class. What I want to do is take that string and stick it on the end of the URL. Does anyone no how I could do this?

Any help would be much appreciated

snippet of activity:

try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);

if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
strReturnedAddress.append(returnedAddress.getLocality()).append("\n");
strReturnedAddress.append(returnedAddress.getPostalCode()).append("\n");

}
myAddress.setText(strReturnedAddress.toString());
String Postcode = returnedAddress.getPostalCode().toString();
}
else{
myAddress.setText("No Address returned!");
}
}
catch (IOException e) {
//TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Cannot get Address!");
}

my http retriever activity:

public class XmlParserActivity extends Activity {

private final String MY_DEBUG_TAG = "WeatherForcaster";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

/* Create a new TextView to display the parsingresult later. */
TextView tv = new TextView(this);
try {
/* Create a URL we want to load some xml-data from. */
URL url = new URL("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=SW1");

/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);

/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */

/* Our ExampleHandler now provides the parsed data to us. */
ParsedExampleDataSet parsedExampleDataSet =
myExampleHandler.getParsedData();

/* Set the result to be displayed in our GUI. */
tv.setText(parsedExampleDataSet.toString());

} catch (Exception e) {
/* Display any Error to the GUI. */
tv.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "WeatherQueryError", e);
}
/* Display the TextView. */
this.setContentView(tv);
}
}
 
Not sure what you are trying to do...

Concatenating string just requires a plus sign +

Code:
String Hi = "Hello " + "World";
You could also use .append(), and StringBuilder and whatnot, but this is only in cases where you need extreme performance.
 
I'm using gps so that wont work as the postcode striing is in a different activity (getPostalCode). Its a weather app I am doing so i need to get the postcode to the API url which is in the xml parser activity.
 
So is your issue passing a string between activities then?

You can add a string to the intent by which you start the next activity.

Something like:

intent.putStringExtra(String name, String value);
 
Ok thanks for pointin me in the right direction. However...got no idea why it still isnt working (force close). Heres the code i added:

Intent intent = new Intent( this,XmlParserActivity.class);

String Postcode = returnedAddress.getPostalCode().toString();

intent.putExtra("postcode", Postcode);

and in my parser I have:

Intent intent = getIntent();

String Postcode = intent.getStringExtra("postcode");

URL url = new URL("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=" + Postcode);

Please note that my gps is working, I have tested it

Thanks again
 
Try logging Postcode to LogCat, what is the value?

Also you should probably look into java naming conventions at some point but that's not a huge deal.

(ie Postcode should be postCode )
 
03-31 16:32:31.060: E/AndroidRuntime(4059): FATAL EXCEPTION: main
03-31 16:32:31.060: E/AndroidRuntime(4059): java.lang.NullPointerException
03-31 16:32:31.060: E/AndroidRuntime(4059): at weather.app.WeatherApplicationActivity.onLocationChanged(WeatherApplicationActivity.java:110)
03-31 16:32:31.060: E/AndroidRuntime(4059): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:228)
03-31 16:32:31.060: E/AndroidRuntime(4059): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
03-31 16:32:31.060: E/AndroidRuntime(4059): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
03-31 16:32:31.060: E/AndroidRuntime(4059): at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 16:32:31.060: E/AndroidRuntime(4059): at android.os.Looper.loop(Looper.java:150)
03-31 16:32:31.060: E/AndroidRuntime(4059): at android.app.ActivityThread.main(ActivityThread.java:4293)
03-31 16:32:31.060: E/AndroidRuntime(4059): at java.lang.reflect.Method.invokeNative(Native Method)
03-31 16:32:31.060: E/AndroidRuntime(4059): at java.lang.reflect.Method.invoke(Method.java:507)
03-31 16:32:31.060: E/AndroidRuntime(4059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
03-31 16:32:31.060: E/AndroidRuntime(4059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
03-31 16:32:31.060: E/AndroidRuntime(4059): at dalvik.system.NativeStart.main(Native Method)
 
Ok thanks for pointin me in the right direction. However...got no idea why it still isnt working (force close). Heres the code i added:

Intent intent = new Intent( this,XmlParserActivity.class);

String Postcode = returnedAddress.getPostalCode().toString();

intent.putExtra("postcode", Postcode);

and in my parser I have:

Intent intent = getIntent();

String Postcode = intent.getStringExtra("postcode");

URL url = new URL("http://new.myweather2.com/developer/forecast.ashx?uac=gcV3ynNdoV&output=xml&query=" + Postcode);

Please note that my gps is working, I have tested it

Thanks again

I've seen instances where instance.getXXExtra() for returns nulls i.e. intent.getStringExtra or getIntExtra etc. Though my experience has been with PhoneState Listener (which BTW also gets an intent back) hence not sure for your specific case, but try something like:

Bundle b = intent.getExtras();
String Postcode = b.getString("postcode");
 
Back
Top Bottom