java restful receive json



have an android application that consumes restful web service type, made with java. From android I call @ Get and perfect. Now I want to make the system login. Send to a @ Post method and verify some fields and return a true to my application or the number of user-id.

android:Login activity to send json.

public class MainActivity extends Activity {
TextView tvIsConnected;
EditText etName,etPass,etTwitter;
Button btnPost;
Person person;
protected void onCreate(Bundle savedInstanceState) {
// get reference to the views
tvIsConnected = (TextView) findViewById(;
etName = (EditText) findViewById(;
etPass = (EditText) findViewById(;

btnPost = (Button) findViewById(;

// check if you are connected or not
tvIsConnected.setText("You are conncted");
tvIsConnected.setText("You are NOT conncted");
public static String POST(String url, Person person){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", person.getName());
jsonObject.accumulate("country", person.getPass());

// 4. convert JSONObject to JSON to String
json = jsonObject.toString();

// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
// 11. return result
return result;
public void onClickbtn(View view) {
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute("");
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
return false;
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {

person = new Person();

return POST(urls[0],person);
// onPostExecute displays the results of the AsyncTask.
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
private boolean validate(){
return false;
else if(etPass.getText().toString().trim().equals(""))
return false;

return true;
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
return result;
With this method, or query, I test my webservice manually. Now I would liketo do it in @ Post and the pass and usuario (user) parameters which will be shipped out in my android json.


public List <Padres> returnTitle4(){

String jpql = "SELECT p FROM Padres p";
Query query = em.createQuery(jpql);
//query.setParameter("pass", "1234");
//query.setParameter("usuario", "William");
List<Padres> resultados = query.getResultList();
return resultados;
Thank you very much and sorry if you do not understand something for my badEnglish level.


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads