如何实现此http post请求的post方法?

时间:2012-06-22 18:12:43

标签: android rest

嗨,这是Android客户端中的HTTP Post Request方法。我不知道如何在Restful Web服务器中实现@POST方法。

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

    }
}

java restful web服务中post方法的实现是什么,这是我的Rest服务器的代码有什么问题?

@POST
@Path("{Login}")
@Consumes({"application/xml"})
public void Add(@PathParam("email") String email,AndroidClientLocation entity) {
    entity.setEmail(email);
    super.create(entity);
}

2 个答案:

答案 0 :(得分: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/.

根据link&amp; link2

向localhost发送请求'表示将其发送到本地计算机。在你的情况下,这将是Android设备。您希望将请求发送到桌面计算机,这是一个远程主机。问题是Appengine dev_sever默认只绑定到本地地址,因此无法远程访问(即,从您的Android设备)。您需要传递--address选项才能从外部访问。检查计算机的IP并将其作为地址传递。类似的东西:

dev_appserver.cmd --address = 192.168.2.220

答案 1 :(得分:0)

多个问题..

  1. 您在服务器端使用什么作为容器
  2. 您的基本网址映射是什么?您的API方法的路径是Login,如何将URL的剩余部分(/resources/rest.android.taxi.androidclientlocation)路由到API。
  3. API使用application / xml,但客户端代码没有发送/设置Content-Type作为application / xml,是否由客户端负责?
  4. 当您从客户端运行请求时,您得到的响应(HTTP错误)是什么。
  5. 您的REST服务器在哪里运行(内部或外部)。
  6. 问题的答案可能会更多地澄清请求。

相关问题