2015-04-15 44 views
0

我有一個MAMP(localhost)數據庫並加載了一個配置文件。我希望能夠加載我的所有配置文件數據,例如我的多行編輯文本中的9個字段。將MAMP(本地主機)數據加載到EditText中發佈Android

沒有錯誤只是我的日誌它顯示成功,當它檢索數據,但它只顯示數據庫中的一個領域,而不是所有......任何想法如何獲得所有?我的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(); 
     } 
    } 

回答

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()); 
    } 

} 
相關問題