0

I am developing an Android application. I am calling a Perl file on a server. This Perl file has different print statements.

I want to make the collective text available to a variable in android Java file of mine.

I have tried this :

URL url= new URL("http://myserver.com/cgi-bin/myfile.pl?var=97320");    

here goes my request to the server file. But how can i get the data from the Perl file available there?

ThomasW
  • 16,981
  • 4
  • 79
  • 106
typedefcoder2
  • 300
  • 1
  • 10
  • 22

1 Answers1

1

In your perl service:

use CGI qw(param header);
use JSON;

my $var = param('var');

my $json = &fetch_return_data($var);

print header('application/json');

print to_json($json); # or encode_json($json) for utf-8

to return data as a JSON object. Then use one of many JSON libraries for Java to read the data. For instance http://json.org/java/:

Integer var = 97320;
InputStream inputStream = new URL("http://myserver.com/cgi-bin/myfile.pl?var=" + var).openStream();
try {
  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
  // Or this if you returned utf-8 from your service
  //BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
  JSONObject json = new JSONObject(readAll(bufferedReader));
} catch (Exception e) {

}
flesk
  • 7,439
  • 4
  • 24
  • 33
  • There is actually a JSON library included with the Android libraries, but it is not the easiest to use: http://developer.android.com/reference/org/json/JSONObject.html also see http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android – ThomasW Nov 13 '11 at 15:29