2016-03-21 6 views
0

私のアンドロイドアプリからリモートのmysqlデータベースにデータを送信する際に「挿入に失敗しました」というエラーがあります。これはSpinnerコントロールを持っています。私はリモートサーバー上のAPIを呼び出してインサートに接続していますが、挿入に失敗します。以下はSpinnerコントロールを持つandroidを使用してリモートのmysqlデータベースにデータを挿入する方法

マイMain Activityコード

public class RCrimeActivity extends AppCompatActivity implements View.OnClickListener{ 

private EditText nop, wh; 
private Button bReport; 
private Spinner ct; 
// Progress Dialog 
private ProgressDialog pDialog; 
// JSON parser class 
JSONParser jsonParser = new JSONParser(); 

private static final String LOGIN_URL = "http://www.nigeriacrimewatch.com/ncwrcapi.php"; 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_MESSAGE = "message"; 

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

    nop = (EditText) findViewById(R.id.nop); 
    wh = (EditText) findViewById(R.id.wh); 
    ct = (Spinner) findViewById(R.id.ct); 
    bReport = (Button) findViewById(R.id.btnReport); 
    bReport.setOnClickListener(this); 

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent homescr = new Intent(RCrimeActivity.this, MainActivity.class); 
      startActivity(homescr); 
     } 
    }); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     case R.id.btnReport: 
      new AttemptReport().execute(); 
      // here we have used, switch case, because on login activity you may //also want to show registration button, so if the user is new ! we can go the //registration activity , other than this we could also do this without switch //case. default: 
     default: 
      break; 
    } 
} 


class AttemptReport extends AsyncTask<String, String, String> { 
    /** 
    * Before starting background thread Show Progress Dialog * 
    */ 

    boolean failure = false; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(RCrimeActivity.this); 
     pDialog.setMessage("Reporting crime..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    @Override 
    protected String doInBackground(String... args) { 
     // here check for success tag 

     int success; 
     String name = nop.getText().toString(); 
     String address = wh.getText().toString(); 
     String type = ct.getSelectedItem().toString(); 


     try { 

      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("nop", name)); 
      params.add(new BasicNameValuePair("wh", address)); 
      params.add(new BasicNameValuePair("ct", type)); 

      Log.d("request!", "starting"); 

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

      // checking log for json response 
      Log.d("Report attempt", json.toString()); 

      // success tag for json 
      success = json.getInt(TAG_SUCCESS); 
      if (success == 1) { 
       Log.d("Crime Reported!", json.toString()); 
       Intent ii = new Intent(RCrimeActivity.this, MapActivity.class); 
       finish(); 
       // this finish() method is used to tell android os that we are done with current //activity now! Moving to other activity 
       startActivity(ii); 
       return json.getString(TAG_MESSAGE); 
      } else { 

       return json.getString(TAG_MESSAGE); 
      } 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    /** 
    * Once the background process is done we need to Dismiss the progress dialog asap * 
    **/ 
    protected void onPostExecute(String message) { 
     pDialog.dismiss(); 
     if (message != null) { 
      Toast.makeText(RCrimeActivity.this, message, Toast.LENGTH_LONG).show(); 
     } 

    } 
} 

APIコード

<?php 
    error_reporting(E_ALL); 
    ini_set('display_errors',TRUE); 

    include_once "conn/nconn.php"; 


    $username=$_POST["username"]; $email=$_POST["email"]; 
    $password=$_POST["password"];   
    if (!empty($_POST)) { 
    if (empty($_POST['username']) || empty($_POST['email']) || empty($_POST['password'])) { 
    // Create some data that will be the JSON response 
    $response["success"] = 0; $response["message"] = "One or both of the fields are empty ."; 
    //die is used to kill the page, will not let the code below to be executed. It will also //display the parameter, that is the json data which our android application will parse to be //shown to the users 
    die(json_encode($response)); 
    } 
$query = " INSERT INTO gmusers (username, email, password, created) values  ('$username','$email','$password', now())"; 
    $sql1=mysql_query($query); if (!empty($sql1)) { $response["success"] = 1; 
    $response["message"] = "Registered sucessfully"; die(json_encode($response)); 
    } else{ 
    $response["success"] = 0; $response["message"] = "Failed to register"; 
    die(json_encode($response)); 
    } 
    } else{ 
    $response["success"] = 0; $response["message"] = " One or both of the fields are empty "; 
    die(json_encode($response)); 
    } 

?> 
+0

ようこそ!あなたもあなたのログのいくつかの関連ビットを追加してくださいできますか? –

+1

あなたはlogcatでどんなエラーになっていますか?また、例外からメッセージを取得しようとします。この行をcatch 'Log.e(" Exception "、" + e.getMessage());に追加してください。 –

+0

私はそれを試してみましょう。ありがとう –

答えて

0

あるサーバーのAPIに問題があるように思えます。あなたのAPIスクリプトを投稿できますか

+0

私はjstが私のAPIを追加しました、それはJavaコードの下です –

関連する問題