简体   繁体   中英

How to implement post method for this http post request ?

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);
}

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

10.0.2.2 Special alias to your host loopback interface (ie, 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 (ie, 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM