Eugen
Eugen

Reputation: 1543

How to make a proper HttpPost to fetch some data from a website?

I'm building an app that requires getting some JSON formatted data from a website, parsing and using it.

I managed to get it working on my home machine (WAMP), but am unable to do the same with the actual website.

The website is www.ace.ucv.ro/android/android.php.

This is what I do:

HttpPost httpPost = new HttpPost("http://ace.ucv.ro/android/android.php");

And it doesn't return anything. Please help me out and tell me what I need to put in there... Is it the server IP or what?

Upvotes: 1

Views: 287

Answers (2)

HandlerExploit
HandlerExploit

Reputation: 8251

Here is a valid example on how to use HttpGet :

String url = "http://www.ace.ucv.ro/android/android.php";

HttpGet httpGet = new HttpGet(url);

String serverResponse = EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity());

JSONObject jsonObject = new JSONObject(serverResponse);

I usually would make it even more compact into something like this:

JSONObject jsonObject = new JSONObject(EntityUtils.toString(new DefaultHttpClient().execute(new HttpGet(url)).getEntity()));

Make sure to declare the correct permission for internet access in your manifest :

<uses-permission android:name="android.permission.INTERNET"/>

Upvotes: 1

Jeremy Smith
Jeremy Smith

Reputation: 1469

If you need to Get data, try HttpGet.

Upvotes: 0

Related Questions