2013-08-31 38 views
0

我正在寻找更新数据库记录的方式。我正在使用android客户端从用户接收输入。 Android客户端通过用c#编写的wcf json与sql数据库进行通信。如何使用wcf http修改/更新数据库行json

我google了一下,也通过stackoverflow搜索,终于知道我可以使用HTTP PUT更新记录。我试图在我的android客户端和c#中实现http PUT方法。它在c#中工作正常(由fiddler检查)。

我想使用json http方法不是xml。因此搜查并没有找到任何好的工作示例。

请帮助我如何在android中实现Http Put json方法。

编辑

DefaultHttpClient httpClient = new DefaultHttpClient(); 
         HttpPut httpPut= new HttpPut("http://10.0.2.2:4806/Service1.svc/UpdateData"); 
         httpPut.setHeader("Accept", "application/json"); 
         httpPut.setHeader("Content-type", "application/json"); 


         JSONStringer getdat = new JSONStringer() 
          .object() 
           .key("pd") 
            .object() 
             .key("ID").value(txtid.getText().toString()) 
             .key("Name").value(txtname.getText().toString()) 
             .key("Project").value(txtproject.getText().toString()) 
             .key("Result").value(txttotal.getText().toString()) 
            .endObject() 
           .endObject(); 


         StringEntity entity = new StringEntity(getdat.toString(),"UTF-8"); 
         entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 


         entity.setContentType("application/json"); 

         httpPost.setEntity(entity); 


         // Send request to WCF service 
         HttpResponse response = httpClient.execute(httpPut); 

         Log.d("WebInvoke", "Saving : " + response.getStatusLine().getStatusCode()); 


         Toast.makeText(this, response.getStatusLine().getStatusCode() + "\n", Toast.LENGTH_LONG).show() ; 



        } 

       } 
     catch(SocketException ex) 
     { 
      Log.e("Error : " , "Error on soapPrimitiveData() " + ex.getMessage()); 
       ex.printStackTrace(); 
      } 

     catch (Exception e) { 
        e.printStackTrace(); 
       } 


     } 
+0

我尝试了一些代码,但没有更新。更新了问题。完全卡在这里,因为它的显示状态为200,但仍未更新数据库中的字段。 – nirmal

回答

1

我解决我的问题,我是不是PUT方法进行串正常。 所以我改变

http://localhost:4806/Service1.svc/UpdateData 

http://localhost:4806/Service1.svc/UpdateData/Result 

已成功更新的数据库!