HTTP PUT请求HTTP、PUT

2023-09-05 05:08:06 作者:妄想

我现在用的是HttpPut与Android的服务器进行通信,响应code我正为500.After先生与服务器的家伙,他说:prepare字符串中包含并发送。

I am using the HttpPut to communicate with server in Android, the response code I am getting is 500.After talking with the server guy he said prepare the string like below and send.

{关键:值,重点:值}

{"key":"value","key":"value"}

现在我完全糊涂了我应该在哪里添加此字符串中的我的要求。

now I am completely confused that where should i add this string in my request.

请帮我了。

推荐答案

我最近不得不想出一个办法让我的Andr​​oid应用程序与WCF服务进行通信,并更新特定的记录。起初,这真的让我很难计算出来,主要是因为我不知道有足够的了解 HTTP 的协议,但我能够创建一个 PUT 使用以下内容:

I recently had to figure out a way to get my android app to communicate with a WCF service and update a particular record. At first this was really giving me a hard time figuring it out, mainly due to me not knowing enough about HTTP protocols, but I was able to create a PUT by using the following:

    URL url = new URL("http://(...your service...).svc/(...your table name...)(...ID of record trying to update...)");

    //--This code works for updating a record from the feed--
    HttpPut httpPut = new HttpPut(url.toString());
    JSONStringer json = new JSONStringer()
    .object() 
       .key("your tables column name...").value("...updated value...")
    .endObject();

    StringEntity entity = new StringEntity(json.toString());
    entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
    entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
    httpPut.setEntity(entity); 

    // Send request to WCF service 
    DefaultHttpClient httpClient = new DefaultHttpClient();

    HttpResponse response = httpClient.execute(httpPut);                     
    HttpEntity entity1 = response.getEntity(); 

    if(entity1 != null&&(response.getStatusLine().getStatusCode()==201||response.getStatusLine().getStatusCode()==200))
    {
         //--just so that you can view the response, this is optional--
         int sc = response.getStatusLine().getStatusCode();
         String sl = response.getStatusLine().getReasonPhrase();
    }
    else
    {
         int sc = response.getStatusLine().getStatusCode();
         String sl = response.getStatusLine().getReasonPhrase();
    }

通过这就是说有通过使用一个库,将生成的更新方法为你让你更新的记录,而无需手动编写code像我上面更容易的选择。 2.库,似乎是常见的有 odata4j 的Restlet 。虽然我一直没能找到一个明确的容易教程 odata4j 有一对的Restlet 这是非常好的:http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx?CommentPosted=true#commentmessage

With this being said there is an easier option by using a library that will generate the update methods for you to allow for you to update a record without having to manually write the code like I did above. The 2 libraries that seem to be common are odata4j and restlet. Although I haven't been able to find a clear easy tutorial for odata4j there is one for restlet that is really nice: http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android-using-restlet.aspx?CommentPosted=true#commentmessage

 
精彩推荐