HttpPost issue in android

528 views android
5

This is my MainActivity.java file in my android project.

I want to receive the HTML source code of a page.

When i run the code the result is Exception : null .

Where is the problem??

public class MainActivity extends AppCompatActivity {

    private HttpPost httppost;
    private HttpResponse response;
    private HttpClient httpclient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textv = (TextView) findViewById(R.id.textv);

        try{

            httpclient=new DefaultHttpClient();

            httppost= new HttpPost("http://example.com/");

            response=httpclient.execute(httppost);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            final String response = httpclient.execute(httppost, responseHandler);

            textv.setText(response);

        }catch(Exception e){
            textv.setText("Exception : " + e.getMessage());
        }
    }
}

answered question

1 Answer

6

if you are really using http://example.com/ , that is because response is null.

posted this

Have an answer?

JD

Please login first before posting an answer.

Ads

Categories