0

Hi this is my HTTP Post Request Method in Android client.i don't know how to implement the @POST method in the Restful web server.

public class AndroidHTTPRequestsActivity extends Activity
{
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Creating HTTP client
    HttpClient httpClient = new DefaultHttpClient();
    // Creating HTTP Post
    HttpPost httpPost = new HttpPost(
            "http://localhost:8080/GPS_Taxi_Tracker_Web_Server/resources/rest.android.taxi.androidclientlocation/Login");

    // Building post parameters
    // key and value pair
    List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2);
    nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com"));
    nameValuePair.add(new BasicNameValuePair("message",
            "Hi, trying Android HTTP post!"));

    // Url Encoding the POST parameters
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
    } catch (UnsupportedEncodingException e) {
        // writing error to Log
        e.printStackTrace();
    }

    // Making HTTP Request
    try {
        HttpResponse response = httpClient.execute(httpPost);

        // writing response to log
        Log.d("Http Response:", response.toString());
    } catch (ClientProtocolException e) {
        // writing exception to log
        e.printStackTrace();
    } catch (IOException e) {
        // writing exception to log
        e.printStackTrace();

    }
}

what is the implementation for the post method in the java restful web service , this is my code for the Rest sever what is wrong ?

@POST
@Path("{Login}")
@Consumes({"application/xml"})
public void Add(@PathParam("email") String email,AndroidClientLocation entity) {
    entity.setEmail(email);
    super.create(entity);
}
Mahmoud
  • 5
  • 1
  • 7
  • What is wrong how? What doesn't work? – Dave Newton Jun 22 '12 at 18:16
  • i am linking the rest with MySQL data base accessed using entity.setEmail(email); super.create(entity); the email is not saved in the data base , but what ever happened what supposed to be the @Post method implementation for the upper post request method. – Mahmoud Jun 22 '12 at 18:18
  • So does the correct data get to your server's Add() method? – Shellum Jun 22 '12 at 18:22

2 Answers2

0

http://developer.android.com/tools/devices/emulator.html#networkaddresses

10.0.2.2 Special alias to your host loopback interface (i.e., 127.0.0.1 on your development machine)

http://localhost:8080/.

as per link & link2

Send a request to localhost' means to send it to the local machine. In your case that would be the Android device. You want to send the request to your desktop machine, which is a remote host. The problem is that the Appengine dev_sever by default only binds to the local address, so it can't be accessed remotely (i.e., from your Android device). You need to pass the --address option to make accessible from the outside. Check your computer's IP and pass it as the address. Something like:

dev_appserver.cmd --address=192.168.2.220

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • i tried my IP address instead didn't work either http://192.168.0.6:8080/GPS_Taxi_Tracker_Web_Server/resources/rest.android.taxi.androidclientlocation/Login – Mahmoud Jun 22 '12 at 18:28
  • try "http://10.0.2.2:8080/GPS_Taxi_Tracker_Web_Server/resources/rest.android.taxi.androidclientlocation/Login – Dheeresh Singh Jun 22 '12 at 18:30
  • sorry but how do i run the whole system , 1- first i run the android code (eclipse) 2- run the rest server (netbeans) 3- check the MYSQL data base thats what i usually do , is that right ?? – Mahmoud Jun 22 '12 at 18:35
  • what about the nameValuePair.add(new BasicNameValuePair("email", "user@gmail.com")); , i am using string email in the @Pathparam – Mahmoud Jun 22 '12 at 18:41
  • can't say about server side http://www.wikihow.com/Execute-HTTP-POST-Requests-in-Android – Dheeresh Singh Jun 22 '12 at 18:52
0

Multiple questions..

  1. What are you using as container on server side
  2. What is your base url mapping. Your API method's path being Login, how do are you routing the remaining part of the URL (/resources/rest.android.taxi.androidclientlocation) to the API.
  3. The API consumes application/xml but the client code is not sending/setting Content-Type as application/xml, is it taken care by the client?
  4. When you run the request from client what is the response (HTTP Error) that you get.
  5. Where is your REST server running (Internal or External).

Answers to the question might clarify the request a bit more.

Tushar Tarkas
  • 1,592
  • 13
  • 25