Android WebService Error while using Android 4.2

0

Hello, I'm developing a project where I need to get and send values to a server. I did everything and it worked perfectly in version 7.0 of the android, but when going to 4.2, it starts giving error.

I'm using an Asynctask to make the request, sending and receiving response from the server.

  public static JSONObject getJSONObjectFromURL(String urlString) throws IOException, JSONException, MalformedURLException, ProtocolException {
    HttpURLConnection urlConnection = null;
    URL url = new URL(urlString);

    urlConnection = (HttpURLConnection) url.openConnection();

    urlConnection.setRequestMethod("GET");
    urlConnection.setReadTimeout(10000 /* milliseconds */ );
    urlConnection.setConnectTimeout(15000 /* milliseconds */ );
    urlConnection.setDoOutput(true);
    urlConnection.connect();

    int statusCode = urlConnection.getResponseCode();

    System.out.println("CODIGOOOOOOOO:"+statusCode);

    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    StringBuilder sb = new StringBuilder();

    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line + "\n");
    }
    br.close();

    String jsonString = sb.toString();
    System.out.println("JSON: " + jsonString);

    return new JSONObject(jsonString);
}

But I'm getting the following error:

java.io.FileNotFoundException: http://192.168.0.12/projeto/Cadastrarformularios.php?json=[{"Modalidade":"Presencial","Unidade":"belem","Email":"[email protected]","CPF":"1","Sobrenome":"sobrenome","Fone":"1","Cod_Consultor":"1001","Data_Cadastro":"2017-11-18 22:49:40","Curso":"autonomia","Nome":"1","Celular":"1"}]

By the time I realized it, the 400 error is returning, but I do not know why it does not work for the android, copying the link the server normally recognizes, and works normally on android 7.0

@edit In version 6.0 also does not catch, only in 7.0

NOTE: For some reason, the user can log in, he can log in normally and the code for authentication is the same one posted. Thanks in advance for the help!

    
asked by anonymous 19.11.2017 / 00:01

1 answer

1

I managed to resolve. I changed the GET method by POST method

 public JSONObject  performPostCall(String requestURL,String parametro) throws JSONException {

    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(15000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);


        OutputStream os = conn.getOutputStream();
        BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(os, "UTF-8"));
        writer.write("json="+parametro);

        writer.flush();
        writer.close();
        os.close();
        int responseCode=conn.getResponseCode();

        if (responseCode == HttpsURLConnection.HTTP_OK) {
            String line;
            BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line=br.readLine()) != null) {
                response+=line;
            }
        }
        else {
            response="";

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return new JSONObject(response);
}

If someone can explain to me why GET does not work I'd be grateful, I'm guessing it's because of the characters [{... in URL

    
19.11.2017 / 00:48