Android:使用webservices将数据插入sql server数据库

时间:2013-10-23 13:25:20

标签: android

我已经完成了结果集的读取,并使用Java Servlet和jtds驱动程序在SQL Server数据库的ListFragment上显示它。这是使用JSON完成的。

现在请指导我如何使用JSON进行Insert \ Delete \ Update操作。

由于

1 个答案:

答案 0 :(得分:0)

我通过以下代码实现了此目的,并使用JSON解析了JSONParser

try {
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost("my_url");
    List parameters = new ArrayList(2);
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("par_1", "1");
    jsonObject.put("par_2", "2");
    jsonObject.put("par_3", "3");
    parameters.add(new BasicNameValuePair("action", "par_action"));
    parameters.add(new BasicNameValuePair("data", jsonObject.toString()));
    httpPost.setEntity(new UrlEncodedFormEntity(parameters));
    HttpResponse httpResponse = httpClient.execute(httpPost);
    Log.v("Server Application", EntityUtils.toString(httpResponse.getEntity())+" "+jsonObject.toString());

} catch (UnsupportedEncodingException e) {
    Log.e("Server Application", "Error: " + e);
} catch (ClientProtocolException e) {
    Log.e("Server Application", "Error: " + e);
} catch (IOException e) {
    Log.e("Server Application", "Error: " + e);
} catch (JSONException e) {
    e.printStackTrace();
}