Pregunta

I'm just trying to get a simple conversor from °C to °F in a Server (Django) The Django Backend is ok, but I only want a simple way to get that info and print that in an Android app:

URL: 192.168.1.212:8000/c_f/4.0

And the response is:

{"far": 39.2}

I tryied, but I only find huge codes and nothing seems to work.

¿Fue útil?

Solución

class sampleService extends AsyncTask<Boolean, Boolean, Boolean> {

    @Override
    protected Boolean doInBackground(Boolean... params) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet del = new HttpGet("192.168.1.212:8000/c_f/4.0");
        del.setHeader("content-type", "application/json");
        try {
            HttpResponse resp = httpClient.execute(del);
            String respStr = EntityUtils.toString(resp.getEntity());
            JSONObject respJSON = new JSONObject(respStr);
            String far = respJSON.getString ("far"); //Check your type data to return in your web service.
            Log.d("Service", far);
        } catch(Exception ex) {
            Log.e("Service","Error!", ex);
        }
        return null;
    }

}

Otros consejos

You can use the httppost and httpClient to enable to get the response from the web

example

String getURL = "192.168.1.212:8000/c_f/4.0";

        HttpClient client = new DefaultHttpClient();  

        HttpPost httppost = new HttpPost(getURL);
HttpResponse responseGet = client.execute(httppost);  
        HttpEntity resEntityGet = responseGet.getEntity(); 
Lod.d("response", EntityUtils.toString(resEntityGet));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top