在android的textview中显示json数据的结果

时间:2016-07-28 10:30:42

标签: php android json

我在Android应用程序中显示数据时遇到一个问题。这是我的php.I现在已编辑以缩短代码。     

if($imei_no!="" && $app_auid!="")
{
        //chk query
        $chk_query=$db1->query("select * from user where imei_number='$imei_no'");
        $chk_cnt=$chk_query->rowCount();
        if($chk_cnt>0)
        {
            $search_qry=$db1->query("select * from candidate_reception cr,candidate_counseling cc ,candidate_admission ca where cr.candidate_id=cc.candidate_id and cr.candidate_id=ca.candidate_id and cr.auid=ca.auid and cr.counseling_id=cc.counseling_id and cr.auid='$app_auid'");
            $ser_count=$search_qry->rowCount();
            if($ser_count>0)
            {
                $stud_row=$search_qry->fetch(PDO::FETCH_OBJ);

                $candidate_id=$stud_row->candidate_id;
                $auid = $stud_row->auid;
                $usn_no = $stud_row->usn_no;
                $sx = $stud_row->candidate_sex


                //image path    
                  if($inst_id==1){ $img_path = "1"; }
                  if($inst_id==2){ $img_path = "2"; }

                $result=array();    
                array_push($result, array('name' => $candidate_name,'img_path'=>$img_path));

            echo json_encode(array("can_data"=>$result));
            echo "Success"          
            }
            else
            {
                echo "Auid not Found.";
            }       
        }
        else
        {
            echo "Invalid IMEI Number.!!!";
        }
}
?>

这是我的java代码:

  private static final String LOGIN_URL = "http://xxx.xxx.x.x/mobile_app/data_push.php";
    private EditText editTextUserName;
    private Button buttonLogin;

    TelephonyManager tel;
    TextView imei;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imei__val);

        editTextUserName = (EditText) findViewById(R.id.editTextAUID);
        buttonLogin = (Button) findViewById(R.id.btn);
        buttonLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });

        tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        imei = (TextView) findViewById(R.id.textView2);
        imei.setText(tel.getDeviceId().toString());

    }

    private void login(){
        String username = editTextUserName.getText().toString().trim();
        String imei_no = imei.getText().toString().trim();
        userLogin(username,imei_no);
    }

    private void userLogin(final String username,final String imei_no){
        class UserLoginClass extends AsyncTask<String,Void,String> {
            ProgressDialog loading;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(IMEI_Val.this,"Please Wait",null,true,true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                if(s=="Success"){

                    Intent intent = new Intent(IMEI_Val.this, CapturePhoto.class);
                   // intent.putExtra(USER_NAME,username);
                    startActivity(intent);
                }

               else{
                   Toast.makeText(IMEI_Val.this, s, Toast.LENGTH_LONG).show();
                    Log.d("Tag Name", "Log Message");
                }
            }

            @Override
            protected String doInBackground(String... params) {
                HashMap<String,String> data = new HashMap<>();
                data.put("auid",params[0]);
                data.put("imei",params[1]);
                RegisterUserClass ruc = new RegisterUserClass();
                String result = ruc.sendPostRequest(LOGIN_URL,data);
                return result;
            }
        }
        UserLoginClass ulc = new UserLoginClass();
        ulc.execute(username,imei_no);
    }

}

我的问题是当点击“登录”按钮时,在有效的用户输入后,我会在学生详细信息中获得json结果。如果用户输入有效,我想转到下一个活动,并显示其他详细信息,如姓名,学院等。我怎样才能做到这一点? 当我输入有效输入时,这里得到json结果作为响应。即使输入正确的regno,我也无法进入下一个活动并在文本框中显示其他详细信息。

3 个答案:

答案 0 :(得分:0)

使用if(&#34; Success&#34; .qualIgnoreCase(s))insted if(s ==&#34; Success&#34;)

答案 1 :(得分:0)

@ user6588225

所以你发送带有成功消息的Json

所以而不是使用==尝试使用contains

所以在onPostExecute(String s)

@Override
protected void onPostExecute(String s) {
     super.onPostExecute(s);
     loading.dismiss();
     if(s.contains("Success")){
         Intent intent = new Intent(IMEI_Val.this, CapturePhoto.class);
         // intent.putExtra(USER_NAME,username);
         startActivity(intent);
     }
     else{
       Toast.makeText(IMEI_Val.this, s, Toast.LENGTH_LONG).show();
       Log.d("Tag Name", "Log Message");
     }
}

答案 2 :(得分:0)

将您在php中的响应更改为此

echo json_encode(array("can_data"=>$result,"success"=>"1"));

在android端将其解析为

JsonObject responseObject = new JsonObject(responseString);
String success=responseObject.getString("success");
if(success.equals("1")){
// go to next activity
}