I have a problem, I made an application that consumes a webservice RestFull made in Visual Studio, when I run the Android Studio application, it consumes without problems, but when I use it on the device (Galaxy S4) or Samsung Tablet, it does not work and always gives error on the same line.
br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
I tried to use logcat to see the java.io.FileNotFoundException: http://192.168.25.115/wsBA.asmx/ConsultarLogin
error in the exception.
The worst thing is that in the emulator it works, I accessed the webservice from another computer that I have to see if it was a matter of access permission, however passing the parameters via GET
direct in browser of the other micro access.
Below is the Actvity code that is giving this error:
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));
}
}
Issue
1) About the permissions this is 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) You are connected to the same wireless network that is here in my room.
3) I already had disabled firewall and antivirus, on the webservice I access from my notebook normally.
O I noticed something else the example I got to do mine works without problem. I already have my error in the device only in 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 the message appears several times in logcat:
Skipped 300 frames! The application may be doing a lot of work on its main thread.
This number 300 varies.