EditText SetText获取空值。从sql通过php检索值

时间:2017-07-25 11:15:27

标签: android

我已经尝试了所有想到的东西,我已经研究了2天了。我是Android新手。我最后的呼救声我转向Stackoverflow上的主人。指导我。

Background.java

protected String doInBackground(String... params) {     
        String update_id = params[1];
        try {
            URL url = new URL(update_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
            String data = URLEncoder.encode("id","UTF-8") + "=" + URLEncoder.encode(update_id, "UTF-8");
            OS.write(data.getBytes());
            bufferedWriter.flush();
            OS.close();
            InputStream is = httpURLConnection.getInputStream();
            is.close();


            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
            String inputLine;
            while((inputLine = bufferedReader.readLine()) != null){
                sb.append(inputLine);
            }

            return "update";}
      @Override
          protected void onPostExecute(String result) {
          AccountDetailsFragment ac = new AccountDetailsFragment();
          ac.receiveDetails(sb);}

AccountDetailsFragment.java

public class AccountDetailsFragment extends Fragment{

EditText update_id, update_address, update_name;
String id = "";
String idd ,namee ,addresss, login_id;
View v;
public AccountDetailsFragment() {
    // Required empty public constructor
}

public void receiveLoginID(String id){
    this.login_id = id;
}

public void receiveDetails(StringBuilder sb){
    String[] strArray = sb.toString().split(Pattern.quote("split"));
    idd= strArray[0];
    namee= strArray[1];
    addresss= strArray[2];

    System.out.println("-*-*--*-*-*-*-*-*-*-*");
    System.out.println(idd);
    System.out.println(namee);
    System.out.println(addresss);
    System.out.println("*-*-*-*-*-*-*-*-*-*-*");

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    LayoutInflater lf = getActivity().getLayoutInflater();
    v = lf.inflate(R.layout.fragment_account_details, container, false);

    update_id = (EditText) v.findViewById(R.id.et_id_edit);
    update_name = (EditText) v.findViewById(R.id.et_Name_edit);
    update_address = (EditText) v.findViewById(R.id.et_address_edit);

    getActivity().setTitle("Account Details");

    Button update = (Button) v.findViewById(R.id.btnUpdate);
    update.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String method = "update";
            Background background = new Background(getContext());
            background.execute(method, login_id);

            System.out.println("*/*/*/*/*/*/*/*/*/*/*/");
            System.out.println(idd);
            System.out.println(namee);
            System.out.println(addresss);
            System.out.println("*/*/*/*/*/*/*/*/*/*/*/");

            update_id.setText(idd.toString());
            update_name.setText(namee.toString());
            update_address.setText(addresss.toString());

        }
    });

    return v;
}}

Update.php

<?php 

     require "init.php";
     $update_id=$_POST["id"];

     $sql_query="select * from test where id = '$update_id'";

     $result = mysqli_query($con,$sql_query);

    if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
       echo $row["id"];
       echo ("split");
       echo $row["name"];
       echo ("split");
       echo $row["address"];
   }
   } else {
        echo $update_id;
   }
  ?>

logcat的

I/System.out: */*/*/*/*/*/*/*/*/*/*/
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: null
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: null
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: null
07-25 15:55:09.367 5914-5914/com.syntillate.aazif.aazifapp I/System.out: 
*/*/*/*/*/*/*/*/*/*/*/

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference
                                                                               at com.syntillate.aazif.aazifapp.AccountDetailsFragment$1.onClick(AccountDetailsFragment.java:115)

2 个答案:

答案 0 :(得分:1)

onPostExecute()更改为此

@Override protected void onPostExecute(String result) { receiveDetails(sb); }

并且如果Background尚未成为内部类,也会使AccountDetailsFragment ac = new AccountDetailsFragment(); ac.receiveDetails(sb);成为内部类。

你这样做了:

AccountDetailsFragment

创建receiveDetails()的新实例并调用if __name__ == '__main__': 它不会改变调用类中的值(idd,namee和地址)。

答案 1 :(得分:1)

你的doInBaground()函数正在返回字符串值“update”。这将是onPostExecute(String result)方法,您将获得结果“Update”。 AccountDetailsFragment类的receiveDetails(StringBuilder sb),因为您使用以下条件拆分字符串。

String [] strArray = sb.toString()。split(Pattern.quote(“split”));

strArray数组变为null,因为split函数不会返回任何关于从onPostExecute()找到的结果的东西。