I need to do a POST request with a Webservice, where I pass a JSON with key and value as a parameter. This request is made on Android, when I make the request it returns me a JSON, with key values called ... ex: field1, field2, field3 ... I needed to create a method where I could pass as parameter in the call only the value of the search key:
Ex: In the POST method I pass:
{
"select":"select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc"
}
And as a return I have:
[
{
"campo1": "3257 | 74327",
"campo2": "Sidnei",
"campo3": "sidnei01",
"campo4": null,
"campo5": 5,
"campo6": null,
"campo7": 7,
"campo8": "74327",
"campo9": "56c07af798f309dbd75822a849ce47b6",
"campo10": "2012-02-08T11:00:06"
}
]
I would like to go to field1 and print on the screen.
The HttpService Class:
package com.example.romeu.cepteste;
import android.os.AsyncTask;
import com.google.gson.Gson;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.Scanner;
/**
* Created by romeu on 28/02/18.
*/
public class HttpService extends AsyncTask<Void, Void, QueryDAO> {
private final String query;
StringBuilder resposta = new StringBuilder();
public HttpService(String query) {
this.query = query;
}
@Override
protected QueryDAO doInBackground(Void... voids) {
if(this.query != null)
{
//Realizar a consulta
try
{
URL url = new URL("https://meuwebsite.com.br/");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.connect();
JSONObject jsonObject = new JSONObject();
jsonObject.put("select", "select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc");
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(jsonObject.toString());
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext())
{
resposta.append(scanner.next());
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (ProtocolException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
return new Gson().fromJson(resposta.toString(), QueryDAO.class);
}
}
QueryDAO Class:
package com.example.romeu.cepteste;
/**
* Created by romeu on 28/02/18.
*/
public class QueryDAO {
public String campo1,campo2,campo3,campo4,campo5,campo6,campo7,campo8,campo9,campo10;
//Getters
public String getCampo1() {
return campo1;
}
public void setCampo1(String campo1) {
this.campo1 = campo1;
}
public String getCampo2() {
return campo2;
}
public void setCampo2(String campo2) {
this.campo2 = campo2;
}
public String getCampo3() {
return campo3;
}
public void setCampo3(String campo3) {
this.campo3 = campo3;
}
public String getCampo4() {
return campo4;
}
public void setCampo4(String campo4) {
this.campo4 = campo4;
}
public String getCampo5() {
return campo5;
}
public void setCampo5(String campo5) {
this.campo5 = campo5;
}
public String getCampo6() {
return campo6;
}
public void setCampo6(String campo6) {
this.campo6 = campo6;
}
public String getCampo7() {
return campo7;
}
public void setCampo7(String campo7) {
this.campo7 = campo7;
}
public String getCampo8() {
return campo8;
}
public void setCampo8(String campo8) {
this.campo8 = campo8;
}
public String getCampo9() {
return campo9;
}
public void setCampo9(String campo9) {
this.campo9 = campo9;
}
public String getCampo10() {
return campo10;
}
public void setCampo10(String campo10) {
this.campo10 = campo10;
}
//String para testes
@Override
public String toString() {
return "QueryDAO{" +
"campo1='" + campo1 + '\'' +
", campo2='" + campo2 + '\'' +
", campo3='" + campo3 + '\'' +
", campo4='" + campo4 + '\'' +
", campo5='" + campo5 + '\'' +
", campo6='" + campo6 + '\'' +
", campo7='" + campo7 + '\'' +
", campo8='" + campo8 + '\'' +
", campo9='" + campo9 + '\'' +
", campo10='" + campo10 + '\'' +
'}';
}
}
Main:
package com.example.romeu.cepteste;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.concurrent.ExecutionException;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnConsultar = (Button) findViewById(R.id.btnConsultar);
final TextView respostaTxt = (TextView) findViewById(R.id.respostaTxt);
btnConsultar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
String query = "select id || ' | ' ||senha_terminal,nome,login_web,null,5,null n2,7,senha_terminal,senha_web,data_inclusao from usuario where id in (3257) order by id desc";
try
{
QueryDAO retorno = new HttpService(query).execute().get();
respostaTxt.setText(retorno.toString());
}
catch (InterruptedException e)
{
e.printStackTrace();
}
catch (ExecutionException e)
{
e.printStackTrace();
}
}
});
}
}
Where is the error that I can not answer?