-4

I am developing a simple login application. My JSON web service returns the username. Here's the JSON code's format:

[{"demologin":{"username":"demoname","id":100}}]

And here is my code:

private class LongRunningGetIO extends AsyncTask <Void, Void, String> {

    protected String getASCIIContentFromEntity(HttpEntity entity) throws IllegalStateException, IOException {
       InputStream in = entity.getContent();
         StringBuffer out = new StringBuffer();
         int n = 1;
         while (n>0) {
             byte[] b = new byte[4096];
             n =  in.read(b);
             if (n>0) out.append(new String(b, 0, n));
         }
         return out.toString();
    }

    protected String doInBackground(Void... params) {
         HttpClient httpClient = new DefaultHttpClient();
         HttpContext localContext = new BasicHttpContext();

         username=txtUserName.getText().toString();
         password=txtPassword.getText().toString();



         String url="MY URL HERE";
         HttpGet httpGet = new HttpGet(url);

         String text = null;
         String result = null;
         String webusername=null;
         int webuserid;
         ArrayList<String> desc=new ArrayList<String>();

         try {

               HttpResponse response = httpClient.execute(httpGet, localContext);
               HttpEntity entity = response.getEntity();
               text = getASCIIContentFromEntity(entity);



                   JSONArray ja = new JSONArray(text) ;
                   // ITERATE THROUGH AND RETRIEVE CLUB FIELDS
                                int n = ja.length();
                                for (int i = 0; i < n; i++) {
                                    // GET INDIVIDUAL JSON OBJECT FROM JSON ARRAY
                                 JSONObject jo = ja.getJSONObject(i);

                                 webusername= jo.getString("demologin");
                                 webuserid= jo.getInt("id");

                                }
         } catch (Exception e) {



                   e.getLocalizedMessage();
                   }    
         return webusername; 
    }   
    protected void onPostExecute(String results) {
        if (results!=null) {

            String sendusername=results;
            Intent inte=new Intent(Login.this,FrontPage.class);
            inte.putExtra("displayusername", sendusername);
            startActivity(inte);


        }
        else
            Toast.makeText(getApplicationContext(), "Invalid user", Toast.LENGTH_LONG).show();


        }
}

The problem here is that I have no idea how to get the username into a string and pass it over the web. Does anyone have an idea about how this could be done?

Jules
  • 14,200
  • 13
  • 56
  • 101
kalyan
  • 1
  • 3

1 Answers1

0

I think this post will help you, here the string manupulation is done on a string returned fro the wsdl, you can use the same string split function to split your results process an array returned by web service

Amalan Dhananjayan
  • 2,277
  • 1
  • 34
  • 47