Android Webservice Error while using Android 4.2

Asked

Viewed 42 times

0

Hello ,I am developing a project where I need to pick up and send values to a server. I did everything and worked perfectly in the version 7.0 of android, but when going to 4.2,begins to give error.

I’m using an Asynctask to request,sending and receiving server response.

  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"}]

From what I noticed, is returning the error 400, but do not know why it does not work by android,copying the link the server recognizes normally,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 login, he can log in normally and the code for authentication is the same posted. Thanks in advance for the help !

1 answer

1

I managed to solve. I switched from the GET method to the 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 anyone can explain to me why GET doesn’t work I’d be grateful,I’m guessing it’s because of the [{... characters in the URL

Browser other questions tagged

You are not signed in. Login or sign up in order to post.