problems when making a request get

1

I explain the situation:

I give permission to use the internet in the manifest and then I want to make an unanswered request by get to a web

If I write (to test) the URL works fine link

But if I write it inside a button in the android studio it does not call

I do it in the following way:

 try {
            url = new URL("http://iva.whatsline.com/Carga.aspx?key=1|542233223322|0");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
               Log.e("respuestaA", String.valueOf(urlConnection));
            } catch (MalformedURLException e) {
               Log.e("respuestaB", "BBB");
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
               Log.e("respuestaC", "CCC");
            }

if you can help me

My logcat says:

  

04-16 15: 56: 16.693 30759-30759 / com.example.notebook.respuestas   D / ViewRootImpl @ db21b33 [MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1

     

04-16 15: 56: 16.695 30759-30759 / com.example.notebook.respuestas   D / ViewRootImpl @ db21b33 [MainActivity]:   mHardwareRenderer.initializeIfNeeded () # 2   mSurface = {isValid = true482020271616}

     

04-16 15: 56: 16.699 30759-30759 / com.example.notebook.respuestas   V / InputMethodManager: Starting input:   tba=android.view.inputmethod.EditorInfo@48d8852 nm:   com.example.notebook.respuestas ic = null

     

04-16 15: 56: 16.699 30759-30759 / com.example.notebook.respuestas   I / InputMethodManager: [IMM] startInputInner -   mService.startInputOrWindowGainedFocus

     

04-16 15: 56: 16.707 30759-30776 / com.example.notebook.respuestas   D / InputTransport: Input channel constructed: fd = 83

     

04-16 15: 56: 16.707 30759-30759 / com.example.notebook.respuestas   V / InputMethodManager: Starting input:   tba=android.view.inputmethod.EditorInfo@1383723 nm:   com.example.notebook.respuestas ic = null

     

04-16 15: 56: 18.116 30759-30759 / com.example.notebook.respuestas   D / ViewRootImpl @ db21b33 [MainActivity]: ViewPostImeInputStage   processPointer 0

     

04-16 15: 56: 18.119 30759-30759 / com.example.notebook.respuestas   W / System: ClassLoader referenced unknown   path: /system/framework/QPerformance.jar

     

04-16 15: 56: 18.12030759-30759 / com.example.notebook.respuestas   E / BoostFramework: BoostFramework (): Exception_1 =   java.lang.ClassNotFoundException: Did not find class   "com.qualcomm.qti.Performance" on   path: DexPathList [[], nativeLibraryDirectories = [/ system / lib64, / vendor / lib64]]

     

04-16 15: 56: 18.120 30759-30759 / com.example.notebook.respuestas   V / BoostFramework: BoostFramework (): mPerf = null

     

04-16 15: 56: 18.26530759-30759 / com.example.notebook.respuestas   D / ViewRootImpl @ db21b33 [MainActivity]: ViewPostImeInputStage   processPointer 1

     

04-16 15: 56: 18.278 30759-30759 / com.example.notebook.respuestas   D / NetworkSecurityConfig: No Network Security Config specified, using   platform default

     

04-16 15: 56: 18.279 30759-30759 / com.example.notebook.respuestas   E / responseA:   com.android.okhttp.internal.huc.HttpURLConnectionImpl: link

    
asked by Mariana 16.04.2018 в 20:30
source

1 answer

0

The answer is not obtained only by converting the value of the connection to String:

   Log.e("respuestaA", String.valueOf(urlConnection));

From the InputStream obtained you can obtain each line of information and store it in StringBuilder :

              try {
                        url = new URL("http://iva.whatsline.com/Carga.aspx?key=1|542233223322|0|mensaje|4|1523047094000|0|1|0|444%22");
                        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                        try {
                            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                            BufferedReader r = new BufferedReader(new InputStreamReader(in));
                            StringBuilder total = new StringBuilder();
                            String line;

                            //Obtiene contenido de la respuesta.
                            while ((line = r.readLine()) != null) {
                                total.append(line).append('\n');
                            }
                            //Imprime respuesta.
                            Log.e("respuestaA", "valor respuesta: " + total.toString());
                        } finally {
                            urlConnection.disconnect();
                        }


                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

Something important is to avoid NetworkOnMainThreadException that is generated when doing tasks in the main thread, therefore you can use a AsyncTask in this way:

    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {

            /**************************************/
            try {
                url = new URL("http://iva.whatsline.com/Carga.aspx?key=1|542233223322|0|mensaje|4|1523047094000|0|1|0|444%22");
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                try {
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                    BufferedReader r = new BufferedReader(new InputStreamReader(in));
                    StringBuilder total = new StringBuilder();
                    String line;

                    //Obtiene contenido de la respuesta
                    while ((line = r.readLine()) != null) {
                        total.append(line).append('\n');
                    }
                    //Imprime respueta
                    Log.e("respuestaA", "valor respuesta: " + total.toString());
                } finally {
                    urlConnection.disconnect();
                }


            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            /**************************************/
       }
    });
    
answered by 16.04.2018 / 21:37
source