I am fairly new to Android and I like to have an inputstream be transferred from one class to another.
Basically, I am trying to get a geocoded location to be sent to a server and a response to be sent to the other class for parsing.
here is a snippet of code I have so far for the httppost and inputstream response:
Location1.java
private void updateWithNewLocation(Location location) {
if (location != null) {
double lat = location.getLatitude();
String lat1 = Double.toString(lat);
double lng = location.getLongitude();
String lng1 = Double.toString(lng);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://...");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair(“lat”, lat1));
nameValuePairs.add(new BasicNameValuePair(“lng”, lng1));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
InputStream is = response.getEntity().getContent();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(20);
int current = 0;
while((current = bis.read()) != -1){
baf.append((byte)current);
}
Here is a snippet from the other class I want to have the inputstream parsed in… There already was an inputsream in this class and I was trying to figure out how to replace it with the other stream from the other class.
parser1.java
//I already have an inputstream here and this is where I want to inject the other class inputstream
InputStream in = httpConnection.getInputStream();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// parse the feed
Document dom = db.parse(in);
Element docEle = dom.getDocumentElement(); ...
Any help would be much appreciated.
I think this is more a
Javaquestion than an Android one. Can’t you just make your existingupdateWithNewLocation()methodpublicand return anInputStreamrather thanvoid? Something like:Location1.java
parser1.java