0

私のAndroidアプリケーションでは、MYSQLからデータを取得したい

Java コードは次のとおりです。

    public class My_Task extends Activity {

        EditText attend_by_txtbx, cus_name_txtbx, contact_no_txtbx, ticket_no_txtbx, task_detail_txtbx;
        Button btnSave;
        TextView cus_name_txt, contact_no_txt, ticket_no_txt, task_detail_txt;
        private static String attend_by;
         String Save = "interval.txt";


        Handler h2 = new Handler();
        Runnable run = new Runnable() {

            @Override
            public void run() {
                EditText cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txtbx);
                ((EditText) cus_name_txtbx).setText(" " + String.valueOf(cus_name_txtbx));
                EditText contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txtbx);
                ((EditText) contact_no_txtbx).setText(" " + String.valueOf(contact_no_txtbx));
                EditText ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txtbx);
                ((EditText) ticket_no_txtbx).setText(" " + String.valueOf(ticket_no_txtbx));
                EditText task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txtbx);
                ((EditText) task_detail_txtbx).setText(" " + String.valueOf(task_detail_txtbx));
                long delaytime = (RestoreRecord0() * 1000);
                h2.postDelayed(this, delaytime);
                String response = CallWebService(cus_name_txtbx, contact_no_txtbx, ticket_no_txtbx, task_detail_txtbx);

            }

            private String CallWebService(EditText cus_name_txtbx,
                    EditText contact_no_txtbx, EditText ticket_no_txtbx,
                    EditText task_detail_txtbx) {
                // TODO Auto-generated method stub
                return null;
            }
        };

        public int RestoreRecord0() {
            try {

                File dir = getFilesDir();
                File file = new File(dir, Save);
                if (!file.exists()) {
                                    }

                FileInputStream fileIS = new FileInputStream(file);
                BufferedReader buf = new BufferedReader(new InputStreamReader(
                        fileIS));
                String readString = new String();
                String data = "";
                // just reading each line and pass it on the debugger

                while ((readString = buf.readLine()) != null) {
                    data += readString;
                }

            }

            catch (Exception ex) {
                ex.getMessage();
            }
            return 0;
        }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_task);

        cus_name_txt = (TextView) findViewById(R.id.cus_name_txt);
        cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txtbx);
        cus_name_txtbx.setText("John");

        contact_no_txt = (TextView)findViewById(R.id.contact_no_txt);  
        contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txtbx);
        contact_no_txtbx.setText("03331231231");

        ticket_no_txt = (TextView)findViewById(R.id.ticket_no_txt);
        ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txtbx);
        ticket_no_txtbx.setText("1");


        task_detail_txt = (TextView)findViewById(R.id.task_detail_txt);
        task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txtbx);
        task_detail_txtbx.setText("Training");

        attend_by_txtbx = (EditText)findViewById(R.id.attend_by_txtbx);
        attend_by_txtbx.setText(My_Task.attend_by);

        Spinner severity = (Spinner) findViewById(R.id.severity_spinner);
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this,
                R.array.Severity_array, android.R.layout.simple_dropdown_item_1line);
        // Specify the layout to use when the list of choices appears
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        severity.setAdapter(adapter3);


        // save button
        btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Onclick_click1(btnSave);

            }
        });   

        long delaytime = (RestoreRecord0() * 1000);
        h2.postDelayed(run, delaytime);


        }

    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback

    } 

    public void Onclick_click1(final Button btnSave)
    {

         final AlertDialog.Builder alert = new AlertDialog.Builder(
                    My_Task.this);

         alert.setTitle("Alert...!");   
         alert.setMessage("Do you want to Save or Cancel");
         alert.setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            // Perform action on click

                            EditText attend_by_txtbx = (EditText) findViewById(R.id.attend_by_txtbx);
                            if(attend_by_txtbx.getText().toString().trim().length() < 1)
                            {
                        Toast.makeText(getBaseContext(),
                                        "Please enter your Name",
                                        Toast.LENGTH_LONG).show();
                            btnSave.performClick();
                            return;
                        }

                            Toast.makeText(getBaseContext(),
                                    "Successfully Saved..!",
                                    Toast.LENGTH_LONG).show();

                        }
                                    });

            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                            dialog.cancel();
                        }
                    });

            alert.show();

    }    


    public String CallWebService(String cid, String cus_name, String contact_number, String ticket_no, String task_detail) {
        String response = new String();
        try {
            String url = "http://192.168.0.29/evisiting_records/read_mytask.php?cus_name="+cus_name+"&contact_number="+contact_number+"&ticket_no="+ticket_no+"&task_detail="+task_detail;
            HttpClient client = new DefaultHttpClient();
            HttpPost req = new HttpPost();
            HttpGet request = new HttpGet();
            URI webservice = new URI(url);
            request.setURI(webservice);
            req.setURI(webservice);
            HttpResponse httpResponse = client.execute(request);
            HttpEntity responseEntity = httpResponse.getEntity();
            if (responseEntity != null) {
                response = EntityUtils.toString(responseEntity);
            }
            HttpResponse httpResponse1 = client.execute(req);
            HttpEntity responseEntity1 = httpResponse1.getEntity();
            if (responseEntity1 != null) {
                response = EntityUtils.toString(responseEntity1);
            }
        }

        catch (Exception e1) 
        {
            e1.printStackTrace();
        }

        return response;
    }      
   }

私のphpコードは次のとおりです。

<?php
// read_My_Task.php

/*
 * Following code will get single task details
 * A task_detail is identified by task id (cid)
 */
 // array for JSON response
$response = array();

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["cid"])) {
    $cid = $_GET['cid'];

    // get a task from my-task table
    $result = mysql_query("SELECT *FROM my_task WHERE cid = $cid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $my_task = array();
            $my_task["cid"] = $result["cid"];
            $my_task["cus_name"] = $result["cus_name"];
            $my_task["contact_number"] = $result["contact_number"];
            $my_task["ticket_no"] = $result["ticket_no"];
            $my_task["task_detail"] = $result["task_detail"];

            // success
            $response["success"] = 1;

            // user node
            $response["my_task"] = array();

            array_push($response["my_task"], $my_task);
            // echoing JSON response
            echo json_encode($response);         
             }            

    else {
            // no task found
          $response["success"] = 0;
        $response["message"] = "No task found";

            // echo no users JSON
      echo json_encode($response);
 }
 } else {
        // no task found
   $response["success"] = 0;
   $response["message"] = "No task found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}

?>

i get out put is:( これじゃない値が欲しい:(

ここに画像の説明を入力

customer_name、contact_no、ticket_no、task_detail を取得する方法を教えてください。Web サービスの呼び出しのどこが間違っていますか?

4

1 に答える 1

0

ええ、コメント セクションにこれを追加することになっていることはわかっていますが、書式設定がないと、あいまいに見えるので、ここにコメントさせてください。

            EditText cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txtbx);
            ((EditText) cus_name_txtbx).setText(" " + String.valueOf(cus_name_txtbx));
            EditText contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txtbx);
            ((EditText) contact_no_txtbx).setText(" " + String.valueOf(contact_no_txtbx));
            EditText ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txtbx);
            ((EditText) ticket_no_txtbx).setText(" " + String.valueOf(ticket_no_txtbx));
            EditText task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txtbx);
            ((EditText) task_detail_txtbx).setText(" " + String.valueOf(task_detail_txtbx));

EditText フィールドを参照して、それらのアドレスを設定するのはなぜですか? スクリーンショットにテキストが表示されるのも不思議ではありません
。 EditText フィールドを参照するには、以下で十分です。

        EditText cus_name_txtbx = (EditText) findViewById(R.id.cus_name_txtbx);
        EditText contact_no_txtbx = (EditText) findViewById(R.id.contact_no_txtbx);
        EditText ticket_no_txtbx = (EditText) findViewById(R.id.ticket_no_txtbx);
        EditText task_detail_txtbx = (EditText) findViewById(R.id.task_detail_txtbx);

必要なのは、json を解析して結果を設定することだけです。
例えば:

String name = // from json
cus_name_txtbx.setText(" " + name);
于 2013-01-07T06:55:12.783 に答える