Ayush Gupta
Ayush Gupta

Reputation: 9295

How to send the JSON response from JS file on request?

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

Upvotes: 3

Views: 2360

Answers (2)

agi
agi

Reputation: 100

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...

Upvotes: 1

Bhavesh
Bhavesh

Reputation: 940

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)

Upvotes: 0

Related Questions