基本的にリモート データベースに接続し、データベースにクエリを実行し、レコードを JSON 形式で返す .net Web サービスを作成しました。ksoap2 ライブラリを使用して Web サービスを使用するために、Android でクライアント側を実行しました。遅いことがわかりました。そして、このリンクからSOAPの代わりにhttp postまたはhttp getを使用できることがわかりました。Web を検索しましたが、http post/get を使用して Web サービスを呼び出すためのコード スニペットやステップ バイ ステップ ガイドが見つかりませんでした。コード スニペットまたはステップ バイ ステップ ガイドを教えてください。
3532 次
2 に答える
2
JSON 応答の使用は非常に簡単です。私のオフィスの同僚の 1 人が、「Android with WCF Services」に関する初心者向けのブログ記事とデモ コードを書いています。
以下は、ソースからのコード スニペットです。
DefaultHttpClient client = new DefaultHttpClient();
// http get request
HttpGet request = new HttpGet(EMPLOYEE_SERVICE_URI
+ evEmployeeId.getText());
// set the hedear to get the data in JSON formate
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
// get the response
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
// if entity contect lenght 0, means no employee exist in the system
// with these code
if (entity.getContentLength() != 0) {
// stream reader object
Reader employeeReader = new InputStreamReader(response.getEntity()
.getContent());
// create a buffer to fill if from reader
char[] buffer = new char[(int) response.getEntity()
.getContentLength()];
// fill the buffer by the help of reader
employeeReader.read(buffer);
// close the reader streams
employeeReader.close();
// for the employee json object
JSONObject employee = new JSONObject(new String(buffer));
}
于 2012-08-29T06:31:42.343 に答える
-1
Web サービスの URL を入力するだけで、正常に動作します :)
URL u = new URL("http://www.blabla.com/Webservice.svc?anyParam=hello¶m2=Hahah");
HTTPURLConnection conn = (HTTPURLConnection) u.openConnection();
InputStream response = conn.getInputStream();
/* Do the rest */
于 2012-08-29T06:32:37.250 に答える