0

I am going to do when user enters 'silent' in edittextbox in android the another textbox will show this two passwords 'umbrella' and 'umbrella12'. I am trying to pass this type of Json

{
   getValueResult: [
            {
               password: "umberlla"
               uname: "silent"
              },
            {
               password: "umberlla12" 
               uname: "silent"
              }]
}

Code for parsing the json is as follows. But I can't get the object getValueResult in StringBuilder after append in while loop, it shows null.

Here My android code:

public void onGoClick(View v) 
    {
        EditText edt=(EditText)findViewById(R.id.editText1);
        HttpClient hc=new DefaultHttpClient();
        HttpPost hp=new HttpPost("http://192.168.0.4:80/WcfClassDatabase/Service1.svc/getValue");
        JSONObject jo=new JSONObject();
        try {
            jo.putOpt("name", edt);
            StringEntity se=new StringEntity(jo.toString());
            hp.setEntity(se);
            hp.setHeader("Accept","application/json");
            hp.setHeader("Content-type","application/json");
            HttpResponse hr= hc.execute(hp);
            Toast.makeText(this,"execute", Toast.LENGTH_LONG).show();

            HttpEntity he=hr.getEntity();
            InputStream is=he.getContent();
            InputStreamReader ir=new InputStreamReader(is);
            BufferedReader br=new BufferedReader(ir);
            StringBuilder sbr=new StringBuilder();
            String line="";
            while((line=br.readLine()) != null)
            {               
                sbr.append(line);
            }

            JSONObject job=new JSONObject(sbr.toString());
            JSONArray ja=job.getJSONArray("getValueResult:");
    //      ArrayList<String> al = new ArrayList<String>();

            String string=  ja.getJSONObject(0).getString("password");
            EditText edt2=(EditText)findViewById(R.id.editText2);
            edt2.setText(string);
        } 
        catch (Exception e) {
            // TODO Auto-generated catch block
            Toast.makeText(this, "oh!Failure", 10).show();
            e.printStackTrace();
        }

    }

I am always got job object null while see in debugging.

Dhaval Shah
  • 618
  • 6
  • 15

1 Answers1

1
 { getValueResult: [2] 0: { password: "umberlla" uname: "silent" } 
1: { password: "umberlla12" uname: "silent" } }

This is not a proper json. Confirm the json first

Fahim
  • 12,198
  • 5
  • 39
  • 57