简体   繁体   中英

How to do I receive result from web service after I post data from Android

I am using JSON http post to send a data to my web service,which is supposed to use the data to do a sqlite query and retrieve a string variable and return it to me. How am I supposed to retrieve the data from the sqlite query?

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();

}
    });
        }

I'm not sure if I understand your problem exactly. But if you want to get the string from server after posting you can do this

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();
    }

In above code, "response" variable is the string you need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM