-3

我有一个应用程序应该将值保存在 sql server 数据库中,但是当我签入我的数据库时,没有保存任何数据,我的代码也没有显示错误。请检查我的代码并相应地更正错误。谢谢你

反馈.java

package com.IwayAfrica.feedback;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import android.util.Log;
import android.os.AsyncTask;
import android.content.DialogInterface;
import android.app.AlertDialog;


public class feedback extends Activity
{
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputcomment;
    RadioGroup sorted;
    RadioGroup ambiance;
    RadioGroup rooms;
    RadioGroup food;
    RadioGroup reception;
    Button buttonsave;
    int success = 0;
    String sort = "";
    String room = "";
    String foods = "";
    String amb = "";
    String rec = "";

    private static String url_register_user = "http://http://localhost/IwayAfrica%20Feedback/processs.php";
    private static final String TAG_SUCCESS = "success";

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

        inputcomment = (EditText) findViewById(R.id.comment);

        Button buttonsave = (Button) findViewById(R.id.btnsave);

        final RadioGroup sorted = (RadioGroup)findViewById(R.id.sorted);
        final RadioButton yes1 = (RadioButton)findViewById(R.id.yes1);
        final RadioButton no1 = (RadioButton)findViewById(R.id.no1);
        final RadioButton par = (RadioButton)findViewById(R.id.par);

        final RadioGroup ambiance = (RadioGroup)findViewById(R.id.ambiance);
        final RadioButton fast = (RadioButton)findViewById(R.id.fast);
        final RadioButton fasten = (RadioButton)findViewById(R.id.fasten);
        final RadioButton slow = (RadioButton)findViewById(R.id.slow);

        final RadioGroup rooms = (RadioGroup)findViewById(R.id.rooms);
        final RadioButton reyes = (RadioButton)findViewById(R.id.reyes);
        final RadioButton reno = (RadioButton)findViewById(R.id.reno);
        final RadioButton remaybe = (RadioButton)findViewById(R.id.remaybe);

        final RadioGroup food = (RadioGroup)findViewById(R.id.food);
        final RadioButton fdbest = (RadioButton)findViewById(R.id.fdbest);
        final RadioButton fdgood = (RadioButton)findViewById(R.id.fdgood);
        final RadioButton fdfair = (RadioButton)findViewById(R.id.fdfair);
        final RadioButton fdpoor = (RadioButton)findViewById(R.id.fdpoor);

        final RadioGroup reception = (RadioGroup)findViewById(R.id.reception);
        final RadioButton rsbest = (RadioButton)findViewById(R.id.rsbest);
        final RadioButton rsgood = (RadioButton)findViewById(R.id.rsgood);
        final RadioButton rsfair = (RadioButton)findViewById(R.id.rsfair);
        final RadioButton rspoor = (RadioButton)findViewById(R.id.rspoor);

        Button send = (Button) findViewById(R.id.btnsave);
        send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 String comment = inputcomment.getText().toString();

                 if (sorted.getCheckedRadioButtonId() == yes1.getId())
                 {
                      sort = "100";
                 }
                 else if (sorted.getCheckedRadioButtonId() == par.getId())
                 {
                      sort = "60";
                 }
                 else if (sorted.getCheckedRadioButtonId() == no1.getId())
                 {
                      sort = "30";
                 }


                 if (ambiance.getCheckedRadioButtonId() == fast.getId())
                 {
                      amb = "100";
                 }
                 else if (ambiance.getCheckedRadioButtonId() == fasten.getId())
                 {
                      amb = "60";
                 }
                 else if (ambiance.getCheckedRadioButtonId() == slow.getId())
                 {
                      amb = "30";
                 }


                 if (rooms.getCheckedRadioButtonId() == reyes.getId())
                 {
                      room = "100";
                 }
                 else if (rooms.getCheckedRadioButtonId() == remaybe.getId())
                 {
                      room = "60";
                 }
                 else if (rooms.getCheckedRadioButtonId() == reno.getId())
                 {
                      room = "30";
                 }


                 if (food.getCheckedRadioButtonId() == fdbest.getId())
                 {
                      foods = "100";
                 }
                 else if (food.getCheckedRadioButtonId() == fdgood.getId())
                 {
                      foods = "75";
                 }
                 else if (food.getCheckedRadioButtonId() == fdfair.getId())
                 {
                      foods = "50";
                 }
                 else if (food.getCheckedRadioButtonId() == fdpoor.getId())
                 {
                      foods = "25";
                 }


                 if (reception.getCheckedRadioButtonId() == rsbest.getId())
                 {
                      rec = "100";
                 }
                 else if (reception.getCheckedRadioButtonId() == rsgood.getId())
                 {
                     rec = "75";
                 }
                 else if (reception.getCheckedRadioButtonId() == rsfair.getId())
                 {
                     rec = "50";
                 }
                 else if (reception.getCheckedRadioButtonId() == rspoor.getId())
                 {
                     rec = "25";
                 }



                     new RegisterNewUser().execute();

            } 
        });
    }


      public void gotomain(View v){
        Intent intent = new Intent(this, main.class);
        startActivity(intent);
      }


class RegisterNewUser extends AsyncTask<String, String, String>{


       protected String doInBackground(String... args) {
           String comment = inputcomment.getText().toString();


           List<NameValuePair> params = new ArrayList<NameValuePair>();
           params.add(new BasicNameValuePair("comment", comment));
           params.add(new BasicNameValuePair("sort", sort));
           params.add(new BasicNameValuePair("amb", amb));
           params.add(new BasicNameValuePair("room", room));
           params.add(new BasicNameValuePair("rec", rec));
           params.add(new BasicNameValuePair("foods", foods));

           JSONObject json = jsonParser.makeHttpRequest(url_register_user,
                   "POST", params);

           // check log cat fro response
           Log.d("Create Response", json.toString());

           // check for success tag
           try {
               int success = json.getInt(TAG_SUCCESS);

               if (success == 1) {
                   // successfully created product
                   Intent i = new Intent(getApplicationContext(), main.class);
                   startActivity(i);

                   // closing this screen
                   finish();
               } else {
                   // failed to create product
               }
           } catch (Exception e)
           {
               e.printStackTrace();
         }
         return null;

       }


   }
}

JSONParser.java

package com.IwayAfrica.feedback;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    // function get json from url
    // by making HTTP POST or GET mehtod
    public JSONObject makeHttpRequest(String url, String method,
            List<NameValuePair> params) {

        // Making HTTP request
        try {

            // check for request method
            if(method == "POST"){
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);
                httpPost.setEntity(new UrlEncodedFormEntity(params));

                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();

            }else if(method == "GET"){
                // request method is GET
                DefaultHttpClient httpClient = new DefaultHttpClient();
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url += "?" + paramString;
                HttpGet httpGet = new HttpGet(url);

                HttpResponse httpResponse = httpClient.execute(httpGet);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
            }           

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

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

进程.php

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['sort']) && isset($_POST['amb']) && isset($_POST['room']) && isset($_POST['foods']) && isset($_POST['rec']) && isset($_POST['comment'])) {

    $problem=$_POST['sort'];
    $time=$_POST['amb'];
    $reccomend=$_POST['room'];
    $rate1=$_POST['foods'];
    $rate2=$_POST['rec'];
    $improve=$_POST['comment'];

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

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

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO feedback (problem,time,reccomend,rate1,rate2,improve)              VALUES('$problem','$time','$reccomend','$rate1','$rate2','$improve')";

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "feedback successfull.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        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);
}
?>
4

2 回答 2

0
$problem=$_POST['sort'];
$time=$_POST['amb'];
$reccomend=$_POST['room'];
$rate1=$_POST['foods'];
$rate2=$_POST['rec'];
$improve=$_POST['comment'];

为什么你不声明 $problem = $_POST['problem'];

对我来说,你的文件太混乱了。

那么您必须尝试该php文件是否成功。尝试从您的浏览器执行。如果不成功,那是你的 php 有问题。

对于单选按钮,试试这个http://nsafaat.wordpress.com/2011/07/27/form-master-entry-data-ke-mysql-server-berbasis-android/

这对我有用。

于 2013-11-13T01:32:38.797 回答
0

我想到了几件事:

1) 检查您的 connect.php 文件。我敢打赌,您的数据库的名称、密码或用户都是错误的。

2) 检查表列名和表名拼写是否正确。

3) 在结果数组中编码错误,以便您可以读取它是什么。

于 2013-11-13T00:56:03.093 回答