我正在尝试使用Json Parsing从网络服务器(wamp)发送和接收数据。在代码中我使用AsynTask来获取json对象及其响应。但它给了我错误。这是代码
public class NewProductActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;
// url to create new product
private static String url_create_product = "http://10.0.2.2/android_connect/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_product);
// Edit Text
inputName = (EditText) findViewById(R.id.inputName);
inputPrice = (EditText) findViewById(R.id.inputPrice);
inputDesc = (EditText) findViewById(R.id.inputDesc);
// Create button
Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);
// button click event
btnCreateProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// creating new product in background thread
new CreateNewProduct().execute();
}
});
}
/**
* Background Async Task to Create new product
* */
class CreateNewProduct extends AsyncTask<String, String, String> {
/* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(NewProductActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
String name = inputName.getText().toString();
String price = inputPrice.getText().toString();
String description = inputDesc.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));
// 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(), AllProductsActivity.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();
}
}
@Override
protected void onStop() {
super.onStop();
if (pDialog != null) {
pDialog.dismiss();
pDialog= null;
}
}
}
这是我的php文件
<?php
$response = array();
// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {
$name = $_POST['name'];
$price = $_POST['price'];
$description = $_POST['description'];
// 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 products(name, price, description) VALUES('$name', '$price', '$description')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// 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);
}
?>
这是错误。
06-28 00:34:01.849: E/AndroidRuntime(479): FATAL EXCEPTION: AsyncTask #1
06-28 00:34:01.849: E/AndroidRuntime(479): java.lang.RuntimeException: An error occured while executing doInBackground()
06-28 00:34:01.849: E/AndroidRuntime(479): at android.os.AsyncTask$3.done(AsyncTask.java:200)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.lang.Thread.run(Thread.java:1096)
06-28 00:34:01.849: E/AndroidRuntime(479): Caused by: java.lang.NullPointerException
06-28 00:34:01.849: E/AndroidRuntime(479): at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:102)
06-28 00:34:01.849: E/AndroidRuntime(479): at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:1)
06-28 00:34:01.849: E/AndroidRuntime(479): at android.os.AsyncTask$2.call(AsyncTask.java:185)
06-28 00:34:01.849: E/AndroidRuntime(479): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)