Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have problem to send JSON array from servlet to android by post method. i tried following code in servlet to send JSON. To Receive from android:

Java
String mode=request.getParameter("mode");
if("login".equals(mode)){
  String name=request.getParameter("username");
  String password=request.getParameter("password");
  System.out.println("recived"+name+","+password);

To send data to android:

Java
`       ArrayList<string> al=new ArrayList<string>();
        ResultSet rs=state.executeQuery(sql);
        while(rs.next())
        {
            al.add(1, rs.getString(0).toString());
            al.add(2, rs.getString(1).toString());
            al.add(3, rs.getString(2).toString());
            al.add(4, rs.getString(3).toString());  
        }
        JSONArray json=new JSONArray();
        json.addAll(al);
        out.print(json);`

and i recived this in android by To send data to servlet

Java
HttpClient client=new DefaultHttpClient();
  HttpPost getmethod= new HttpPost(url);
  ArrayList<namevaluepair> namevaluepair=new ArrayList<namevaluepair>(3);
  namevaluepair.add(new BasicNameValuePair("mode","login"));
  namevaluepair.add(new BasicNameValuePair("username",name.getText().toString()));
  namevaluepair.add(new BasicNameValuePair("password",password.getText().toString()));
  getmethod.setEntity(new UrlEncodedFormEntity(namevaluepair));
  HttpResponse httpResponse = client.execute(getmethod);

To receive data from servlet

Java
HttpEntity httpEntity = httpResponse.getEntity();
 InputStream is = httpEntity.getContent();
 BufferedReader reader = new BufferedReader(new InputStreamReader(
                              is, "iso-8859-1"), 8);
 StringBuilder sb = new StringBuilder();
 String line = null;
      while ((line = reader.readLine()) != null) {
                         sb.append(line + "\n");
                }
               is.close();
               String  json = sb.toString();
               JSONArray json1=new JSONArray();
              jObj = new JSONObject(json);
                 try {
                      // Getting Array of Contacts
                      LOGIN = jsonobj.getJSONArray(NAME);

                      // looping through All Contacts
                      for(int i = 0; i < LOGIN.length(); i++){
                          JSONObject c = LOGIN.getJSONObject(i);
                          // Storing each json item in variable
                          String id = c.getString(USERNAME);
                          String name = c.getString(CATEGORY);
                          String email = c.getString(PASSWORD);
                          result.setText(email+name+id);
                             }
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }


i recived data on servlet but i can't get data in android please help..
Posted
Updated 11-Oct-17 18:18pm
v2

1 solution

Send Data Like this from Servlet



ArrayList<ModelClassDonateFood> al=new ArrayList();
       ModelClassDonateFood m;
           ResultSet rs=d.Select(sql);
           while(rs.next())
           {

               String id=rs.getString(1);
               String name=rs.getString(2);
               String gender=rs.getString(3);
               String age=rs.getString(4);
               String bldgrp=rs.getString(5);
               String disease=rs.getString(6);
               m=new ModelClassDonateFood(id, name, gender, bldgrp,disease,age);
               al.add(m);
           }

           String json=new Gson().toJson(al);
           response.getWriter().write(json);
           System.out.println("---------------------------------------->"+json);
       }catch(Exception e){

           System.out.println(e);

       }

ModelClassDonateFood Class

/**
 *
 * @author Ajith VM
 */
public class ModelClassDonateFood {
    private String fooditem;
    private String time;
    private String date;
    private String name;
     private String bloodgrp;
     private String age;

    public String getAge() {
        return age;
    }

    public ModelClassDonateFood(String fooditem, String time, String date, String name, String bloodgrp, String age) {
        this.fooditem = fooditem;
        this.time = time;
        this.date = date;
        this.name = name;
        this.bloodgrp = bloodgrp;
        this.age = age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public ModelClassDonateFood(String fooditem, String time, String date, String name, String bloodgrp) {
        this.fooditem = fooditem;
        this.time = time;
        this.date = date;
        this.name = name;
        this.bloodgrp = bloodgrp;
    }

    public void setBloodgrp(String bloodgrp) {
        this.bloodgrp = bloodgrp;
    }

    public String getBloodgrp() {
        return bloodgrp;
    }

    public ModelClassDonateFood(String fooditem, String time, String date, String name) {
        this.fooditem = fooditem;
        this.time = time;
        this.date = date;
        this.name = name;
    }

    public String getFooditem() {
        return fooditem;
    }

    public String getTime() {
        return time;
    }

    public String getDate() {
        return date;
    }

    public String getName() {
        return name;
    }

    public void setFooditem(String fooditem) {
        this.fooditem = fooditem;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public void setName(String name) {
        this.name = name;
    }
        
}

Android Receve Like THis

public class ViewCriticalMembers extends AsyncTask<Void,Void,String> {
        String res="";
        int tmp;


        @Override
        protected String doInBackground(Void... voids) {
            try {
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(uRl);
                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                res = EntityUtils.toString(httpEntity);
            }catch(Exception e)
            {
                System.out.print("................................................>"+e);

            }
            return res.trim();
        }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        if (s != null) {
            System.out.print(s);
            try {
                JSONArray jsonArray = new JSONArray(s.trim());
                if (jsonArray != null) {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        id = jsonObject.getString("fooditem");
                        name = jsonObject.getString("time");
                        age = jsonObject.getString("age");
                        gender=jsonObject.getString("date");
                        blood=jsonObject.getString("name");
                        diseace=jsonObject.getString("bloodgrp");
                        al.add(new ModelClass(id,name,age,gender,blood,diseace));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            CustomeAdaptor customeAdaptor=new CustomeAdaptor(al,getApplicationContext());
            ViewCriticalPatiants.setAdapter(customeAdaptor);

        } else {
            Log.e("JSON Data", "didnt receve any data from the server");
        }

    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900