Device error(Samsung Galaxy S4) when running app, but in Emulator works!

Asked

Viewed 210 times

1

I have a problem, I made an application that consumes a webservice Restfull made in Visual Studio, when running the application of Android Studio, consumes without problems, but when I use the device(Galaxy S4) or Samsung Tablet, it does not work and gives error always in the same line.

br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

I tried to use the logcat to see the error java.io.FileNotFoundException: http://192.168.25.115/wsBA.asmx/ConsultarLogin in exception.

The worst thing is that in the emulator works, I stopped the webservice from another computer I have to see if it was a matter of permission to access, but passing the parameters via GET right in the browser of the other micro access.

Below follows code from Actvity that’s making this mistake:

package br.com.intelider.bomapetiteandroid;

import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;


/**
 * Created by gleyson on 19/04/2015.
 */
public class login_activity extends ActionBarActivity {
   EditText edLogin;
   EditText edSenha;
   Button btnLogar;
   TextView txtBemVindo;
   TextView txtNumUser;
   Integer userId;
   //LoginService loginService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //retirando a barra do relogio
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //inicio
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_login);

        btnLogar = (Button) findViewById(R.id.btLogar);
        edLogin = (EditText) findViewById(R.id.edLogin);
        edSenha = (EditText) findViewById(R.id.edSenha);
        txtBemVindo = (TextView) findViewById(R.id.textBemVindo);
        txtNumUser = (TextView) findViewById(R.id.textViewHost);


        final Button btnLogar = (Button) findViewById(R.id.btLogar);
        btnLogar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btLogar:
                        String txtUsr = edLogin.getText().toString();
                        String txtPwd = edSenha.getText().toString();
                        if (!txtUsr.equals("") && !txtPwd.equals("")) {
                            String restURL = "http://192.168.25.115/wsBA.asmx/ConsultarLogin";
                            new RestOperation().execute(restURL);
                        } else {
                            Toast.makeText(login_activity.this, "Insira o usuário e senha!", Toast.LENGTH_LONG).show();

                        }
                        break;
                    default:
                        break;
                }

            }
         });

        sqlite_configuracao con = new sqlite_configuracao(this);
        configuracao_class config = con.buscarTodosConfig();
        if (config != null) {
            TextView etHost = (TextView) findViewById(R.id.textViewHost);
            etHost.setText(String.valueOf(config.getHost()));
        }
    }

    private class RestOperation extends AsyncTask<String, Void, Void> {

        final HttpClient httpClient = new DefaultHttpClient();
        String content;
        String error;
        ProgressDialog progressDialog = new ProgressDialog(login_activity.this);
        String intLogin = "";
        String strSenha = "";
        TextView serverDataReceived = (TextView) findViewById(R.id.serverDataReceived);
        TextView showParsedJSON = (TextView) findViewById(R.id.showParsedJSON);
        EditText userinput = (EditText) findViewById(R.id.edLogin);
        EditText pawdinput = (EditText) findViewById(R.id.edSenha);



        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            progressDialog.setTitle("Por favor aguarde ...");
            progressDialog.show();

            try {
                intLogin += "&" + URLEncoder.encode("intLogin", "UTF-8") + "=" + userinput.getText();
                strSenha += "&" + URLEncoder.encode("strSenha", "UTF-8") + "=" + pawdinput.getText();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        @Override
        protected Void doInBackground(String... params) {
            BufferedReader br = null;

            URL url;
            try {
                url = new URL(params[0]);

                URLConnection connection = url.openConnection();
                connection.setDoOutput(true);

                 OutputStreamWriter outputStreamWr = new OutputStreamWriter(connection.getOutputStream());
                outputStreamWr.write(intLogin + strSenha);
                outputStreamWr.flush();

    //******* Linha onde dá erro **********
                br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

                StringBuilder sb = new StringBuilder(10000);
                String line = null;


                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append(System.getProperty("line.separator"));
                }

                content = sb.toString();

            } catch (MalformedURLException e) {
                error = e.getMessage();
                e.printStackTrace();
            } catch (IOException e) {
                error = e.getMessage();
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            progressDialog.dismiss();

            if (error != null) {
                serverDataReceived.setText("Error " + error);
            } else {
                serverDataReceived.setText(content);

                String output = "";
                JSONObject jsonResponse;

                try {
                    jsonResponse = new JSONObject(content);

                    JSONArray jsonArray = jsonResponse.optJSONArray("Android");

                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject child = jsonArray.getJSONObject(i);

                        String par_id = child.getString("PAR_ID_23");
                        String par_razao = child.getString("PAR_RAZAO_SOCIAL_23");
                        String par_senha = child.getString("PAR_SENHA_23");

                        output = "Name = " + par_id + System.getProperty("line.separator") + par_razao + System.getProperty("line.separator") + par_senha;
                        output += System.getProperty("line.separator");

                    }

                    showParsedJSON.setText(output);

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_login, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_Configuracoes:
                CarregaTelaConfiguracao();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    public void CarregaTelaConfiguracao(){
        startActivity(new Intent(this, configuracao_activity.class));
    }

  }

Editing

1) About permissions this way in androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

2)It is connected on the same wireless network that is here in my room

3) I had already disabled firewall and antivirus, on the webservice I access my notebook normally.

O Checked another thing the example I took to make mine works without problem. Already my error only in the device on the line:

br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

I noticed one more thing: that my application does and the example does not, when it runs appears the message several times in logcat:

Skipped 300 frames! The application may be Doing Too Much work on its main thread.

This number 300 varies.

  • hi @felipe, debugging and searching saw that the error comes from getInputStream(), is a generic exception of java.io, what bothers me is that it only occurs on the device and not on the emulator, I’m reading the java documentation to see if I can find a solution. I put the same version 4.4.2, but the error still persists the same way.

2 answers

3

The first thing I would do to find out where the problem is is to check the permissions of the app, if you are allowed to access the internet.

The second would be to check if the device is on the same network as your "server".

Another thing would be to check if you have any locked ports on your server.

1


  • Good afternoon, Debugging and searching saw that the error comes from getInputStream(), is a generic exception of java.io, what bothers me is that it only occurs on the device and not on the emulator, I’m reading the java documentation to see if I can find a solution. I put the same version 4.4.2, but the error still persists the same way.

  • I got Felipe thank you very much, the problem was really what Oce said the line Connection.setDoOutput(true), however it did not work, then searching I saw that I had to clarify that the method was POST dai I added Connection.setRequestMethod("POST") and it worked, Thanks brother!

Browser other questions tagged

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