2013-08-24 57 views
0

ocuring我在post_item.java文件下面的代碼:錯誤,同時連接到Android和PHP與MySQL

package com.iwantnew.www; 

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.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class post_item extends Activity { 
    private ProgressDialog pDialog; 
    JSONParser jsonParser = new JSONParser(); 
    Button add_room; 
    EditText contact_no; 
    EditText no_of_room; 
    EditText price_per_room; 
    EditText description; 

    private static String url_create_product = "http://10.0.2.2/android_iwant/android_add_room.php"; 

    private static final String TAG_SUCCESS = "success"; 

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

     //contact_no = (EditText) findViewById(R.id.contact_no); 
     no_of_room = (EditText) findViewById(R.id.no_of_room); 
     price_per_room = (EditText) findViewById(R.id.price_per_room); 
     //description = (EditText) findViewById(R.id.description); 
     contact_no = (EditText) findViewById(R.id.contact_no); 

     add_room = (Button) findViewById(R.id.add_room); 

     add_room.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // creating new product in background thread 
       new add_new_room().execute(); 
      } 
     }); 
    } 
// suru... 
    class add_new_room extends AsyncTask<String, String, String> { 

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

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 
      //String contact = contact_no.getText().toString(); 
      String quantity = no_of_room.getText().toString(); 
      String price = price_per_room.getText().toString(); 
      //String contact = contact_no.getText().toString(); 
      String descriptions = description.getText().toString(); 

      // Building Parameters 
      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      //params.add(new BasicNameValuePair("contact", contact)); 
      params.add(new BasicNameValuePair("quantity", quantity)); 
      params.add(new BasicNameValuePair("price", price)); 
      params.add(new BasicNameValuePair("descriptions", descriptions)); 

      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_create_product, 
        "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(), MainActivity.class); 
        startActivity(i); 

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

      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      // dismiss the dialog once done 
      pDialog.dismiss(); 
     } 

    } 
} 

而且,我已經在我的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['quantity']) && isset($_POST['price'])) { 

    //$location = $_POST['location']; 
    $quantity = $_POST['quantity']; 
    $price = $_POST['price']; 
    //$productID = $_POST['area']; 
    //$contact = $_POST['contact']; 
    $descriptions = $_POST['descriptions']; 

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

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

    // mysql inserting a new row 
     $result = mysql_query("INSERT INTO room_tb(uid,categoryID,quantity,price,description) VALUES(5,1,'$quantity','$price','$descriptions')"); 
    //$result1 = mysql_query("INSERT INTO users(userContactNumber) VALUES('$contact')"); 

    // check if row inserted or not 
    if($result)/*&& ($result1)*/ { 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = "Room added successfully."; 

     // 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); 
} 
?> 

每當我發送說明或聯繫號碼到數據庫,android模擬器說「意外停止工作」,沒有記錄進入數據庫。但是,如果我只通過quantitypricecategoryIDuid,則不會發生錯誤,並且記錄會發送到數據庫。我究竟做錯了什麼?

這裏是我的日誌貓鏈接​​3210

+0

添加的logcat轉儲,請。 – dst

+0

@dst這裏鏈接到我的logcat http://pastebin.com/WvBPs2gs – enjal

回答

0
Caused by: java.lang.NullPointerException 
07-15 02:06:36.366: E/AndroidRuntime(2918):  at com.iwantnew.www.post_item$add_new_room.doInBackground(post_item.java:96) 

所以,無論是線96,它是零,應該檢查。有您的codesnippet沒有行號,但我認爲這與使用說明變量做:當賦值給變量描述註釋設置

String descriptions = description.getText().toString(); 

//description = (EditText) findViewById(R.id.description); 
+0

no no ...我在我的java文件中有未註釋的描述..但它仍然顯示相同的問題 – enjal

+0

什麼是現在的異常stacktrace? –

+0

那條線上有什麼? –