如何从Android发布数据后从Web服务收到结果

时间:2013-05-28 01:43:47

标签: android asp.net json web-services

我正在使用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();

}
    });
        }

1 个答案:

答案 0 :(得分: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();
    }

在上面的代码中,"响应"变量是你需要的字符串。