0

我正在使用 JSON http post 向我的 Web 服务发送数据,该服务应该使用数据进行 sqlite 查询并检索字符串变量并将其返回给我。我应该如何从 sqlite 查询中检索数据?

try
{
HttpClient client = new DefaultHttpClient();  

HttpPost post = new HttpPost("http://server/NCPS/trial.aspx");   

post.setHeader("Content-type", "application/json");

post.setHeader("Accept", "application/json");

JSONObject obj = new JSONObject();

obj.put("ic", "s9412953b");

post.setEntity(new StringEntity(obj.toString(), "UTF-8"));

HttpResponse response = client.execute(post);  

}catch(IOException e)

{

System.out.println("Error " + e.getMessage()); 

} catch (JSONException e) {

// TODO Auto-generated catch block

System.out.println("Error " + e.getMessage()); 

}

}

});trd.start();

}
    });
        }
4

1 回答 1

0

我不确定我是否完全理解你的问题。但是,如果您想在发布后从服务器获取字符串,您可以这样做

HttpClient client = new DefaultHttpClient();
    HttpResponse httpResponse;

    try {
        httpResponse = client.execute(request);
        responseCode = httpResponse.getStatusLine().getStatusCode();
        message = httpResponse.getStatusLine().getReasonPhrase();

        HttpEntity entity = httpResponse.getEntity();

        if (entity != null) {

            InputStream instream = entity.getContent();
            response = convertStreamToString(instream);

            // Closing the input stream will trigger connection release
            instream.close();
        }

    } catch (ClientProtocolException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    } catch (IOException e) {
        client.getConnectionManager().shutdown();
        e.printStackTrace();
    }

在上面的代码中,“response”变量是你需要的字符串。

于 2013-05-28T02:03:00.157 回答