-1
I have this method in the main:
private String token () throws IOException {
String login = this.login.getText().toString();
String senha = this.senha.getText().toString();
Login dados = new Login(login, senha);
HttpService HttpService = new HttpService("https://www.acweb.net.br/api/orcamentos/login");
String retorno = HttpService.execute(dados).toString();
return retorno;
}
That calls a class Httpservice extending Asynctask next:
package br.net.concertacell.classes;
import android.os.AsyncTask;
import com.google.gson.Gson;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Scanner;
public class HttpService extends AsyncTask<Login, Integer, String> {
private String url;
public HttpService (String _url) {
this.url = _url;
}
@Override
protected String doInBackground(Login... parametros) {
StringBuilder resposta = new StringBuilder();
try {
URL url = new URL(this.url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String json = new Gson().toJson(parametros);
System.out.println("retorno: " + json);
connection.setRequestMethod("POST");
connection.getOutputStream().write(json.getBytes("UTF-8"));
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.connect();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext()) {
resposta.append(scanner.next());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return resposta.toString();
}
}
The problem:
Linebound System.out.println("return: " + json);, I have the following answer:
I/System.out: retorno: [{"id":0,"login":"Teste","senha":"Testando"}]
That is, a json, which is what the API waiting.
But when it passes to the lines below:
connection.setRequestMethod("POST");
connection.getOutputStream().write(json.getBytes("UTF-8"));
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
connection.setConnectTimeout(5000);
connection.connect();
then the system crashes.
There’s something wrong with that code?
I tried it the way below and it also gives the same error:
public String post(String json) throws IOException {
URL url = new URL("https://www.acweb.net.br/api/orcamentos/login");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "application/json");
connection.setRequestProperty("Accept", "application/json");
connection.setDoOutput(true);
PrintStream printStream = new PrintStream(connection.getOutputStream());
printStream.println(json);
connection.connect();
String jsonDeResposta = new Scanner(connection.getInputStream()).next();
return jsonDeResposta;
}
@Override
protected String doInBackground(Login... parametros) {
StringBuilder resposta = new StringBuilder();
String json = new Gson().toJson(parametros);
try {
this.post(json);
} catch (IOException e) {
e.printStackTrace();
}
return resposta.toString();
}
It seems to me that’s the way to send the JSON:
[{"id":0,"login":"Teste","senha":"Testando"}]
But I’m not sure!