Reputation: 1684
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.
Upvotes: 0
Views: 1167
Reputation: 4798
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;
}
}
Upvotes: 1
Reputation: 2774
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:
Upvotes: 2