I am testing with HttpGet to simply see if it would download anything from my website, but the application crashes on startup. Anyone know why?

package com.httptest;

import java.net.URI;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import org.apache.http.client.methods.HttpRequestBase;


public class httptest extends Activity {
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button  recieve = (Button)findViewById(R.id.Button);
        recieve.setOnClickListener((OnClickListener) this);
    }
    
    @SuppressWarnings("null")
	public void onClick(View v) {
        String url = "";
        HttpRequestBase http = null;
        http.setURI(URI.create(url));
    }
}

Dani AI

Generated

A crash that happens before the UI is drawn almost always means an exception was thrown during onCreate or while inflating the layout. was right to push for the LogCat stack trace — that will show the exact exception and line number. Common causes for this thread: casting the Activity to an OnClickListener when it does not implement that interface (ClassCastException), calling a method on a null reference (NullPointerException), or doing network I/O on the UI thread on newer Android releases (NetworkOnMainThreadException).

Use an explicit listener or implement the interface on the Activity, and move network work off the UI thread. Example: attach a listener with an anonymous inner class and start a background task from there.

Button load = findViewById(R.id.Button);
load.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
    // start background network task (do not run HTTP on UI thread)
  }
});

Run HTTP calls in a background thread (AsyncTask, Executor, or a library). A minimal AsyncTask pattern:

private class FetchTask extends AsyncTask<String,Void,String> {
  protected String doInBackground(String... urls) {
    HttpURLConnection conn = (HttpURLConnection) new URL(urls[0]).openConnection();
    try (InputStream in = conn.getInputStream()) {
      return readStream(in); // implement a safe reader
    } finally {
      conn.disconnect();
    }
  }
  protected void onPostExecute(String result) {
    textView.setText(result);
  }
}

Checklist:

  • Inspect LogCat for the full stack trace (look for ClassCastException / NullPointerException).
  • Ensure correct layout IDs and that setContentView(...) matches the layout used.
  • Add the permission if missing:
    <uses-permission android:name="android.permission.INTERNET" />
  • Avoid network calls on the UI thread; follow Android network guidance: Network on Main Thread and background work.

These steps resolve the startup crash most often and will make the HTTP fetch work reliably across Android versions.

Recommended Answers

All 6 Replies

I did some more Googling on it. And this is my newest code. It still crashes.

package com.httptest;

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;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;



public class httptest extends Activity {
    /** Called when the activity is first created. */
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
     
        Button load = (Button)findViewById(R.id.Button);
        load.setOnClickListener((OnClickListener) this);
    }
	
	public void onClick(View v) {
        TextView text = (TextView)findViewById(R.id.Text);
		getData(text);
	}
    

	public void getData(TextView content) {
		String url = "";
		HttpClient client = new DefaultHttpClient();
		HttpGet request = new HttpGet(url);
		
		try {
			@SuppressWarnings("unused")
			HttpResponse response = client.execute(request);
			content.setText("Loaded content!");
		}
		catch(Exception ex) {
			content.setText("Failed to load content!");
		}
	}
}

Why you are printing some silly text message instead of getting stack trace of what ever exception throw?

To make sure that it wasn't the type of the HttpResponse variable being put into the TextView crashing it.

Well that message can let you know something is wrong, however summary from stack trace will indicate what is reason...

Well it crashes on startup. The button or anything doesn't even get drawn when it crashes.

It could be somewhere else. Would you like to take a look at the other parts of my project?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.