Android で HttpPut を使用してサーバーと通信しています。応答コードは 500 です。
{"キー":"値","キー":"値"}
今、この文字列をリクエストのどこに追加すればよいか、完全に混乱しています。
私を助けてください 。
最近、Android アプリが WCF サービスと通信し、特定のレコードを更新する方法を見つけなければなりませんでした。HTTP
最初は、主にプロトコルについて十分に知らないために、これを理解するのに本当に苦労しましたPUT
が、次を使用してを作成できました。
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();
}
そうは言っても、更新メソッドを生成するライブラリを使用することで、上記のようにコードを手動で記述しなくてもレコードを更新できる、より簡単なオプションがあります。odata4j
よくあると思われる 2 つのライブラリはとrestlet
です。私は明確で簡単なチュートリアルを見つけることができませんでしたがodata4j
、restlet
それは本当に素晴らしいものです: http://weblogs.asp.net/uruit/archive/2011/09/13/accessing-odata-from-android -using-restlet.aspx?CommentPosted=true#commentmessage