将MAMP(Localhost)数据加载到EditText问题Android

时间:2015-04-15 16:33:43

标签: java php android android-edittext localhost

我有一个MAMP(localhost)数据库,并加载了个人资料。我希望能够加载我的所有个人资料数据,就像我的多行编辑文本中的9个字段一样。

只有我的日志没有错误它在检索数据时显示成功,但它只显示数据库中的一个字段而不是全部......任何想法如何获得全部?我的PHP和其他一切都很好,因为我测试过它。

我想知道你是否可以帮助我?

我的班级:

  String pid;

    // Progress Dialog
    private ProgressDialog pDialog;

    // JSON parser class
    JSONParser jsonParser = new JSONParser();

    // single product url
    private static final String url_get_single_profile = "http://MYIP:8888/android_connect/get_all_profiles.php";


    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_USERPROFILE = "UserProfile";
    private static final String TAG_PID = "pid";
    private static final String TAG_FIRSTNAME = "firstname";
    private static final String TAG_LASTNAME = "lastname";
    private static final String TAG_ADDRESS = "address";
    private static final String TAG_COMMENTS = "comments";
    private static final String TAG_AGE = "age";
    private static final String TAG_GENDER = "gender";
    private static final String TAG_HEIGHT = "height";
    private static final String TAG_WEIGHT = "weight";
    private static final String TAG_INFORMATION = "information";


    Button btnSendSMS;
    EditText txtPhoneNo;
    EditText txtMessage;



    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_send_sms);
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);



        // getting product details from intent
        Intent i = getIntent();

        // getting product id (pid) from intent
        pid = i.getStringExtra(TAG_PID);

        // Getting complete product details in background thread
        new GetProfileDetails().execute();

        btnSendSMS.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString() + displayLocation();
                displayLocation();

                if (phoneNo.length()>0 && message.length()>0)
                    sendSMS(phoneNo, message);
                else
                    Toast.makeText(getBaseContext(),
                            "Please enter both phone number and message.",
                            Toast.LENGTH_SHORT).show();
            }
        });


    }

    private String displayLocation(){
            LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, new LocationListener(){
                @Override
                public void onStatusChanged(String s, int i, Bundle bundle) {}
                @Override
                public void onProviderEnabled(String s) {}
                @Override
                public void onProviderDisabled(String s) {}
                @Override
                public void onLocationChanged(final Location location) {}
            });
            Location myLocation = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
            double longitude = myLocation.getLongitude();
            double latitude = myLocation.getLatitude();
           return "https://www.google.co.id/maps/@"+latitude+","+longitude;
    }





    //---sends a SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {

        PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, Home.class), 0);
            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, pi, null);


        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";

        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS sent",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Generic failure",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case android.telephony.gsm.SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(SENT));

        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered",
                                Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        }, new IntentFilter(DELIVERED));

        android.telephony.gsm.SmsManager smms = android.telephony.gsm.SmsManager.getDefault();
        smms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
    }


    /**
     * Background Async Task to Get complete product details
     * */
    class GetProfileDetails extends AsyncTask<String, String, JSONObject> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(SendSMS.this);
            pDialog.setMessage("Loading Profile details. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Getting product details in background thread
         * */
        protected JSONObject doInBackground(String...param) {
            // Check for success tag
            int success;
            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pid", pid));

                // getting product details by making HTTP request
                // Note that product details url will use GET request
                JSONObject json = jsonParser.makeHttpRequest(
                        url_get_single_profile, "GET", params);

                // check your log for json response
                Log.d("Single Product Details", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully received product details
                    JSONArray productObj = json
                            .getJSONArray(TAG_USERPROFILE); // JSON Array

                    // get first product object from JSON Array
                    JSONObject product = productObj.getJSONObject(0);

                    // instead return your product to onPostExecute
                    return product;
                } else {
                    // product with pid not found
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(JSONObject product) {
            if (product != null) {
                // product with this pid found
                // Edit Text
                txtMessage = (EditText) findViewById(R.id.txtMessage);


                // display profile data in EditText
                try {
                    txtMessage.setText(product.getString(TAG_FIRSTNAME));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_LASTNAME));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_ADDRESS));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_COMMENTS));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_AGE));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_GENDER));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_HEIGHT));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_WEIGHT));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                try {
                    txtMessage.setText(product.getString(TAG_INFORMATION));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }

1 个答案:

答案 0 :(得分:0)

您在每个项目上覆盖了EditText字段。 要修复它,只需创建一个StringBuilder并连接每个可用的项目。 然后,在您提取完所有数据后,在底部调用txtMessage.setText

protected void onPostExecute(JSONObject product) {
    if (product != null) {
        // product with this pid found
        // Edit Text
        txtMessage = (EditText) findViewById(R.id.txtMessage);

        StringBuilder jsonStringBuilder = new StringBuilder(); //Create StringBuilder for concatenation of JSON results

        // display profile data in EditText
        try {
            //txtMessage.setText(product.getString(TAG_FIRSTNAME));  //Don't set the text here
            jsonStringBuilder.append(product.getString(TAG_FIRSTNAME)); //Concatenate each separate item
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_LASTNAME));
            jsonStringBuilder.append(product.getString(TAG_LASTNAME));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_ADDRESS));
            jsonStringBuilder.append(product.getString(TAG_ADDRESS));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_COMMENTS));
            jsonStringBuilder.append(product.getString(TAG_COMMENTS));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_AGE));
            jsonStringBuilder.append(product.getString(TAG_AGE));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_GENDER));
            jsonStringBuilder.append(product.getString(TAG_GENDER));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_HEIGHT));
            jsonStringBuilder.append(product.getString(TAG_HEIGHT));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_WEIGHT));
            jsonStringBuilder.append(product.getString(TAG_WEIGHT));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            //txtMessage.setText(product.getString(TAG_INFORMATION));
            jsonStringBuilder.append(product.getString(TAG_INFORMATION));
            jsonStringBuilder.append(System.getProperty("line.separator"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        txtMessage.setText(jsonStringBuilder.toString());
    }

}