3

I have a JS file which generates a JSON object.

This JSON response string will be parsed in my android app after it is recievedfrom the URL of my JS file using this function.

public JSONObject getJSONFromUrl(String url) {

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
 }

where url is the URL of the JS file from which the JSON object has to be fetched.

However, I can not figure out how to send the JSON response from my JS file.

So ,once I have created the JSON object, how am I supposed to properly return that JSON reponse from my JS file so that it can be fetched?

EDIT : I am hosting it on Amazon Web Services, so I can install whatever Web Server softare is required to perform the task on my EC2 instance

EDIT 2 The JS is basically the Google feed API returned in JSON result format,which needs to be fetched in my android app

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
  • Where are you hosting that js file? Do you have a server running NodeJs or something? Javascript is a client side scripting language, without knowledge about your server no one can give you an answer. – Xaver Kapeller Mar 06 '16 at 13:49
  • I am hosting it on Amazon Web Services, so I can install whatever Web Server softare is required to perform the task on my EC2 instance – Ayush Gupta Mar 06 '16 at 13:51
  • Can you show us the relevant parts of your Javascript? If it's part of a website than it obviously won't work. If you really want to use javascript to write your server application you have to use something like NodeJS. – Xaver Kapeller Mar 06 '16 at 13:55
  • The JS is basically the [Google feed API](https://developers.google.com/feed/v1/devguide) returned in [JSON result format](https://developers.google.com/feed/v1/devguide#resultJson),which needs to be fetched in my android app – Ayush Gupta Mar 06 '16 at 14:03
  • try `jObj = new JSONObject(sb.toString());`. it may work. – ELITE Mar 06 '16 at 14:20
  • The issue is not creating the JSON object, but transmitting it. – Ayush Gupta Mar 06 '16 at 14:25

2 Answers2

1

I would always try to avoid JS on the server-side. Of course you can run JS on your server with node.js for example, but I would only do this, if there is no other option.

So are you really sure you need JS on your server? You can use the Google feed API in many other languages (take a look at this Google JSON guide). You can directly access it in your Android Application (this is the Java-example from the Google JSON Guide, the android-code looks a bit different):

URL url = new URL("https://ajax.googleapis.com/ajax/services/feed/find?" +
              "v=1.0&q=Official%20Google%20Blog&userip=INSERT-USER-IP");
URLConnection connection = url.openConnection();
connection.addRequestProperty("Referer", /* Enter the URL of your site here */);

String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while((line = reader.readLine()) != null) {
   builder.append(line);
}

JSONObject json = new JSONObject(builder.toString());
// now have some fun with the results...

Or you can do this with php, if you want to preprocess the API-response on your server:

$url = "https://ajax.googleapis.com/ajax/services/feed/find?" .
       "v=1.0&q=Official%20Google%20Blog&userip=INSERT-USER-IP";

// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);

// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
agi
  • 100
  • 6
0

You can use node.js or express.js to return response.

Use JSON.stringify(objToJson)) you will get {"response":"value"} as response

Sorry i am no expert in this area but you might want to look into these links.

Proper way to return JSON using node or Express

Responding with a JSON object in NodeJS (converting object/array to JSON string)

Community
  • 1
  • 1
Bhavesh
  • 882
  • 2
  • 9
  • 18