使用php

时间:2018-03-05 05:15:14

标签: php android mysql

  

我试图在数据库中创建单列的总和,但它会继续显示列的第一行,而不是在列中添加所有列并在我的 textview 中显示它:txtkg。

我想要这个

的总数

enter image description here

这是显示第一行的当前应用

enter image description here

    public class MainActivity extends AppCompatActivity {

    TextView txtkg;
    HttpResponse httpResponse;
    JSONObject jsonObject = null;
    String StringHolder = "" ;
    String HttpURL = "http://192.168.254.7/midterm/sumkg.php";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        txtkg = (TextView) findViewById(R.id.textView3);

        new GetDataFromServerIntoTextView(MainActivity.this).execute();

    }
  

//使用AsyncTask声明GetDataFromServerIntoTextView方法。

   public class GetDataFromServerIntoTextView extends AsyncTask<Void, Void, Void>
   {
  

//声明语境。

       public Context context;


       public GetDataFromServerIntoTextView(Context context)
       {
           this.context = context;
       }

       @Override
       protected void onPreExecute()
       {
           super.onPreExecute();
       }

       @Override
       protected Void doInBackground(Void... arg0)
       {

           HttpClient httpClient = new DefaultHttpClient();
  

//将HttpURL添加到我的HttpPost对象中。

           HttpPost httpPost = new HttpPost(HttpURL);

           try {
               httpResponse = httpClient.execute(httpPost);

               StringHolder = EntityUtils.toString(httpResponse.getEntity(), "UTF-8");

           } catch (ClientProtocolException e) {
               e.printStackTrace();
           } catch (IOException e) {
               e.printStackTrace();
           }

           try{
  

//将字符串持有者变量传递给JSONArray。

               JSONArray jsonArray = new JSONArray(StringHolder);
               jsonObject = jsonArray.getJSONObject(0);


           } catch ( JSONException e) {
               e.printStackTrace();
           }

           catch (Exception e)
           {
               // TODO Auto-generated catch block
               e.printStackTrace();
           }
           return null;
       }
       protected void onPostExecute(Void result)
       {
           try {
  

//完成加载后将JSOn字符串添加到textview。

               txtkg.setText(jsonObject.getString("kg"));

           } catch (JSONException e) {
  

// TODO自动生成的捕获块

               e.printStackTrace();
           }
       }
   }
  

sumkg.php

<?php
include 'DatabaseConfig.php';

// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);

if ($conn->connect_error) {

 die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT SUM(kg) FROM kahitano";

$result = $conn->query($sql);

if ($result->num_rows >0) {


 while($row[] = $result->fetch_assoc()) {

 $tem = $row;

 $json = json_encode($tem);

 }

} else {
 echo "No Results Found.";
}
 echo $json;
$conn->close();
?>
  

DatabaseConfig.php

<?php

//Define your host here.
$HostName = "localhost";

//Define your database username here.
$HostUser = "root";

//Define your database password here.
$HostPass = "";

//Define your database name here.
$DatabaseName = "db_baggage";

?>

0 个答案:

没有答案