MuhammadNe
MuhammadNe

Reputation: 704

How to consume json object that is sent from android to server in java?

I am trying to pass JSON object from android app using HTTP post to heroku server and then get it back, but I keep getting response code : 404, I don't know if the problem is from the client side or the server side, here is the client side HTTP connection :

class JsonAsyncTask extends AsyncTask<Void, Void, String> {

    private final String USER_AGENT = "Mozilla/5.0";

    protected void onPreExecute() {

    }

    @Override
    protected String doInBackground(Void... params) {


        try {
            String url = "https://jce-blb.herokuapp.com/test";
            URL obj = new URL(url);
            HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

            //add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("Accept", "application/json");
            con.setRequestProperty("Content-type", "application/json");


            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name", "Android");

            // Send post request
            con.setDoOutput(true);
            DataOutputStream wr = new DataOutputStream(con.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();

            int responseCode = con.getResponseCode();
            System.out.println("\nSending 'POST' request to URL : " + url);              
            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(
            new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //print result
            System.out.println(response.toString());
        } catch (Exception e) {

        }
        return null;
    }

    protected void onPostExecute(String content) {
    }
}

And this is server side in heroku, I used java :

import java.sql.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;
import java.net.URI;
import java.net.URISyntaxException;
import static spark.Spark.*;
import spark.template.freemarker.FreeMarkerEngine;
import spark.ModelAndView;
import static spark.Spark.get;
import com.heroku.sdk.jdbc.DatabaseUrl;

public class Main {

  public static void main(String[] args) {

    port(Integer.valueOf(System.getenv("PORT")));
    staticFileLocation("/public");

    get("/test", (req,res) -> {
      return "im back";
    });
  }
}

Upvotes: 1

Views: 145

Answers (1)

wero
wero

Reputation: 33010

The client sends a POST request to /test but the server only defines a GET handler for that route. (Imho the server should responds with a 405 method not allowed, but maybe this is how Spark responds).

Therefore try to also define a handler for POST requests.

Upvotes: 1

Related Questions