ユーザーがAndroidのedittextboxに「silent」と入力すると、別のテキストボックスにこの2つのパスワード「umbrella」と「umbrella12」が表示されます。このタイプのJsonを渡そうとしています
{
getValueResult: [
{
password: "umberlla"
uname: "silent"
},
{
password: "umberlla12"
uname: "silent"
}]
}
json を解析するコードは次のとおりです。しかし、while ループに追加した後、StringBuilder でオブジェクト getValueResult を取得できません。null が表示されます。
ここに私のアンドロイドコード:
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();
}
}
デバッグ中に表示されると、常にジョブ オブジェクトが null になります。