0

So I have never used HTTP post before and I need to send a response and receive a response from the same. Here is the documentation I am provided:

EDIT: This code sample comes from Microsoft Azure Machine Learning and I have to integrate into my Android app to do data prediction.

URL

https://**api-version=2.0&details=true

Request

  "Inputs": {
    "input1": {
      "ColumnNames": [
        "Light",
        "Proximity",
        "Ax",
        "Ay",
        "Az",
        "Gx",
        "Gy",
        "Gz"
      ],
      "Values": [
        [
          "0",
          "0",
          "0",
          "0",
          "0",
          "0",
          "0",
          "0"
        ],
        [
          "0",
          "0",
          "0",
          "0",
          "0",
          "0",
          "0",
          "0"
        ]
      ]
    }
  },
  "GlobalParameters": {}
}

Sample Response

{
  "Results": {
    "output1": {
      "type": "DataTable",
      "value": {
        "ColumnNames": [
          "Scored Probabilities for Class \"BackPocket\"",
          "Scored Probabilities for Class \"Ear\"",
          "Scored Probabilities for Class \"Handbag\"",
          "Scored Probabilities for Class \"SidePocket\"",
          "Scored Labels"
        ],
        "ColumnTypes": [
          "Numeric",
          "Numeric",
          "Numeric",
          "Numeric",
          "Categorical"
        ],
        "Values": [
          [
            "0",
            "0",
            "0",
            "0",
            "BackPocket"
          ],
          [
            "0",
            "0",
            "0",
            "0",
            "BackPocket"
          ]
        ]
      }
    }
  }
}

How can I send this Post Request in Java/Android and then process the response?

I am totally new to this hence I have no idea. I did search about HTTP POST etc. but couldn't find enough on sending and receiving responses. Any library suggestion is also much appreciated.

Thanks.

Jishan
  • 1,654
  • 4
  • 28
  • 62

2 Answers2

2

There a lot of HTTP connection libraries. You can user any of them:

  • OkHttp - very powerful and simple library. You can find a lot of examples on its' page

  • Retrofit library comes with OkHttp and wrap requests into interfaces Retrofit Link

To deal with json, I recommend you to use GSON library

So the basic algorithm to deal with your question is the following:

  • Create GSON model file with getters and setters
  • Create HTTP call with method you need
  • Make call and receive request (do it not in UI thread)
  • Parse request with created getters/setters
Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
1

Here is an example request using Google's Volley for the http request and Gson for converting the json data to an object. You would need to create pojos that represent the json data if you plan to use Gson.

Your input data is quite messy, I did not attempt to convert that from a pojo, that's an exercise for the reader ;)

String request = "{\"Inputs\":{\"input1\":{\"ColumnNames\":[\"Light\",\"Proximity\",\"Ax\",\"Ay\",\"Az\",\"Gx\",\"Gy\",\"Gz\"],\"Values\":[[\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"],[\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\",\"0\"]]}},\"GlobalParameters\":{}}";

        Context appContext = InstrumentationRegistry.getTargetContext();
        String url ="https://**api-version=2.0&details=true";

        RequestQueue queue = Volley.newRequestQueue(appContext);

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
              new Response.Listener<String>() {
                  @Override
                  public void onResponse(String response) {

                      Log.d("TAG", "success: " + response);
                      String jsonResponse = response;
                      Gson gson = new Gson();
                      Stuff stuffObject = gson.fromJson(jsonResponse, Stuff.class);

                  }
              }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        queue.add(stringRequest);

Pojo objects generated with jsonSchemaToPojo

Stuff.class

public class Stuff {

    @SerializedName("Results")
    @Expose
    private Results results;

    public Results getResults() {
        return results;
    }
    public void setResults(Results results) {
        this.results = results;
    }
}

Results.class

public class Results {

    @SerializedName("output1")
    @Expose
    private Output1 output1;

    public Output1 getOutput1() {
        return output1;
    }

    public void setOutput1(Output1 output1) {
        this.output1 = output1;
    }
}

Output1.class

public class Output1 {

    @SerializedName("type")
    @Expose
    private String type;
    @SerializedName("value")
    @Expose
    private Value value;

    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    public Value getValue() {
        return value;
    }
    public void setValue(Value value) {
        this.value = value;
    }
}

Value.class

public class Value {

    @SerializedName("ColumnNames")
    @Expose
    private List<String> columnNames = new ArrayList<String>();
    @SerializedName("ColumnTypes")
    @Expose
    private List<String> columnTypes = new ArrayList<String>();
    @SerializedName("Values")
    @Expose
    private List<List<String>> values = new ArrayList<List<String>>();

    public List<String> getColumnNames() {
        return columnNames;
    }
    public void setColumnNames(List<String> columnNames) {
        this.columnNames = columnNames;
    }
    public List<String> getColumnTypes() {
        return columnTypes;
    }
    public void setColumnTypes(List<String> columnTypes) {
        this.columnTypes = columnTypes;
    }
    public List<List<String>> getValues() {
        return values;
    }
    public void setValues(List<List<String>> values) {
        this.values = values;
    }

}
Gary Bak
  • 4,746
  • 4
  • 22
  • 39
  • Volley was deprecated so i don't think its a good idea to recommend it – Anton Kazakov Sep 14 '16 at 16:34
  • @AntonKazakov do you have a reference for that claim? i use `compile 'com.android.volley:volley:1.0.0'` and see no deprecation warnings. – Gary Bak Sep 14 '16 at 17:28
  • Maybe I'm wrong. I was using volley long time ago and last time i opened its repo on github mcxiaoke/android-volley it says its deprecated. But if you are compiling its from google mean I'm wrong. But anyway OkHttp library doing better :) – Anton Kazakov Sep 14 '16 at 17:31
  • I find Volley easier to use than Retrofit for simple http requests. In addition, Volley can be configured to use OkHttp if required. – Gary Bak Sep 14 '16 at 17:57
  • But you can use clean OkHttp w/o retrofit ;) – Anton Kazakov Sep 14 '16 at 18:01