1

I'm a android noob on programming, but with some help of a few programms I can learn the basics. I would like to do a basic http get request to an arduino ethernetshield.

For this I've found some code, but I can't get it to work. I'm allways stuck on the getResponse part with the code I've tried from several pages.

I've found the following page which gave me readable code: How to work with an image using url in android?

Now I've created the following: Press on a button and do a get to an url:


package adhttpget.test;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;



public class AdhttpgetActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


public void pushbutton1(View view) {
    Toast.makeText(getBaseContext(), "button press", Toast.LENGTH_SHORT).show();
            Log.e("button", "pressed");

             URL connectURL = new URL("http://192.168.0.48/?out=13&status=2");
             HttpURLConnection conn = (HttpURLConnection)connectURL.openConnection(); 

             // do some setup
             conn.setDoInput(true); 
             conn.setDoOutput(true); 
             conn.setUseCaches(false); 
             conn.setRequestMethod("GET"); 

             // connect and flush the request out
             conn.connect();
             conn.getOutputStream().flush();

             // now fetch the results
             String response = getResponse(conn);    // <-- THIS IS MY PROBLEM



}
private String getResponseOrig(HttpURLConnection conn)
{
    InputStream is = null;
    try 
    {
        is = conn.getInputStream(); 
        // scoop up the reply from the server
        int ch; 
        StringBuffer sb = new StringBuffer(); 
        while( ( ch = is.read() ) != -1 ) { 
            sb.append( (char)ch ); 
        } 
        return sb.toString(); 
    }
    catch(Exception e)
    {
       Log.e("http", "biffed it getting HTTPResponse");
    }
    finally 
    {
        try {
        if (is != null)
            is.close();
        } catch (Exception e) {}
    }

    return "";
}

}

Where can I find information to learn how to write the code correctly? Or do you happen to have the answer in some kind of hint so I can learn from it?

Community
  • 1
  • 1
Gaston
  • 307
  • 4
  • 14
  • don't say you are a noob in android :). It doesn't help the question and answers and it feeds the SO dilemma: a closed elitist form of trolling – quinestor Nov 27 '12 at 14:19
  • I think I understand what you mean. Never thought about it that way :) – Gaston Nov 27 '12 at 20:17

1 Answers1

1

You must create a BufferedReader passing the InputStream, then you can read strings

private static String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

Then I recommend you make the connection (or read/write file) with a separeted thread from the Thread UI (use Thread, AsyncTask, Handler, etc) because that will improve your app.

http://developer.android.com/intl/es/guide/components/processes-and-threads.html

xagema
  • 775
  • 1
  • 7
  • 14