Consume local web service from my android device

0

I am working with a Web Service made in ASP.NET, the problem is that I can not run the url localhost from my android device, I used the IP address of my computer in this way in the mobile browser: link or link but do not there's an answer, I also tried disabling the firewall.

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_info_user);

    mensaje= (TextView)findViewById(R.id.textView8);
    info = (TextView)findViewById(R.id.textView9);

    btnBuscar = (Button)findViewById(R.id.btnBuscar);
    btnBuscar.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v)
        {
            new ObtenerDatosUsuario().execute("localhost:8433/odata/Usuarios");
        }
    });
}

private class ObtenerDatosUsuario extends AsyncTask<String, String, String>
{
    HttpURLConnection connection = null;
    BufferedReader reader = null;

    @Override
    protected String doInBackground(String... params)
    {

        try
        {
            URL url = new URL(params[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream stream = connection.getInputStream();
             reader = new BufferedReader(new InputStreamReader(stream));

            StringBuffer resultado = new StringBuffer();

            String linea = "";
            while ((linea = reader.readLine()) != null) {
                resultado.append(linea);
            }

            String resultadoString = resultado.toString();

            JSONObject resultadoJSON = new JSONObject(resultadoString);


            int id = resultadoJSON.getInt("Id");
            String nombre = resultadoJSON.getString("Nombre");
            String correo = resultadoJSON.getString("Email");

            return "Id: " + id + " - " + "Nombre: " + nombre + " - " + "Correo: " + correo;

        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally {
            if(connection != null)
            {
                connection.disconnect();
            }
            try
            {
                if(reader != null)
                {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return null;
    }

    @Override
    protected void onPostExecute(String result)
    {
        Log.i("ServicioRest","onPostExecute");
        Mensaje.setText(result);
    }
    @Override
    protected void onPreExecute()
    {
        Log.i("ServicioRest","onPreExecute");
        info.setText("Se esta obteniendo la información.");
    }
}

What am I doing wrong?

    
asked by Richard Mancilla 29.04.2017 в 10:29
source

1 answer

1

Well, you could use the Apache HTTP libraries to facilitate the work and then use it to make a web request:

In this case, you have to send your URL object to the HttpURLConnection object and open the connection and specify the type of the method (Get) and pick up the answer and you already treat it.

 public  String petitionHTTPGET(String page) {
    String response = "";

    URL url;
    HttpURLConnection connection = null;
    InputStream inputStream = null;

    try {           
        url = new URL(page);        
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        inputStream = connection.getInputStream();          
        response = getStringFromInputStream(inputStream);

    } catch (MalformedURLException e) {
        Log.e("RESTExample", "MalformedURLException");
    } catch (ProtocolException e) {
        Log.e("RESTExample", "ProtocolException");
    } catch (IOException e) {
        Log.e("RESTExample", "IOException");
    }finally{
        connection.disconnect();
        try {
            inputStream.close();
        } catch (IOException e) {
            Log.e("RESTExample", "IOException");
        }
    }

    return response;
}

In this other example you would make a POST request and you would treat the returned lines, which in the example go in XML.

    private String httpPostRequest(String httpPost, String xmlToSend) {     

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(httpPost);

    try {
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("xml", xmlToSend));

        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));

        String line = "";
        String xml = "";
        while ((line = rd.readLine()) != null) {
            xml += line;
        }
        return xml;
    } catch (IOException e) {
        Log.e("RESExample", "IOException");
    }
    return "";
}
    
answered by 29.04.2017 в 12:02