我正在做一些Android应用程序的工作,我希望应用程序使用json和POST将数据添加到我的mysql数据库。
logcat的:
11-15 17:29:44.928: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41edce60)
11-15 17:29:44.938: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41ee92e8)
11-15 17:29:44.938: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41edc4e8)
11-15 17:29:44.948: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41e6f988)
11-15 17:29:44.958: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41e663d0)
11-15 17:29:44.958: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41ec8308)
11-15 17:29:44.958: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41ea75e0)
11-15 17:29:44.978: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41e8a590)
11-15 17:29:44.978: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41e6b478)
11-15 17:29:44.988: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41f0d378)
11-15 17:29:44.998: E/DataHolder(1059): Internal data leak within a DataBuffer object detected! Be sure to explicitly call close() on all DataBuffer extending objects when you are done with them. (internal object: com.google.android.gms.common.data.DataHolder@41f14648)
我的活动:
public class AddLocationActivity extends Activity {
EditText edClubName, edAddress, edAge, edDescription;
String sClubName, sAddress, sAge, sDescription;
double lat, lng;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static String url_create_product = "http://000100023.host56.com/db_create.php";
private static final String TAG_SUCCESS = "success";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_location);
ActionBar abs = getActionBar();
abs.setTitle(R.string.activity_title_add_location);
abs.setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class CreateNewProduct extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddLocationActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating product
* */
protected String doInBackground(String... args) {
edClubName = (EditText) findViewById(R.id.edClubName);
edAddress = (EditText) findViewById(R.id.edAddress);
edAge = (EditText) findViewById(R.id.edAge);
edDescription = (EditText) findViewById(R.id.edDescription);
sClubName = edClubName.getText().toString();
sAddress = edAddress.getText().toString();
sAge = edAge.getText().toString();
sDescription = edDescription.getText().toString();
try {
addLocation(edAddress);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String sLat = String.valueOf(lat);
String sLng = String.valueOf(lng);
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", sClubName));
params.add(new BasicNameValuePair("location", sAddress));
params.add(new BasicNameValuePair("description", sDescription));
params.add(new BasicNameValuePair("description", sAge));
params.add(new BasicNameValuePair("lat", sLat));
params.add(new BasicNameValuePair("lng", sLng));
// 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
Log.d("LOG_TAG", "Done");
// closing this screen
finish();
} else {
// failed to create product
Log.d("LOG_TAG", "Failed");
}
} 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();
}
}
public void addLocation (View v) throws IOException {
Geocoder mGc = new Geocoder(this);
List<Address> mList = mGc.getFromLocationName(sAddress, 1);
Address mAddress = mList.get(0);
String locality = mAddress.getLocality();
lat = mAddress.getLatitude();
lng = mAddress.getLongitude();
//Toast.makeText(this, "Lat: " + lat + " Lng: " + lng, Toast.LENGTH_LONG).show();
}
public void cancelAddLocation () {
}
}
我希望用户添加数据,它使用Geocode获取lat和long的位置,然后使用php脚本将其添加到数据库中:
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['name']) && isset($_POST['location']) && isset($_POST['age']) && isset($_POST['rating']) && isset($_POST['description']) && isset($_POST['lat']) && isset($_POST['lng'])) {
$name = $_POST['name'];
$age = $_POST['age'];
$rating = $_POST['age'];
$description = $_POST['description'];
$location = $_POST['location'];
$lat = $_POST['lat'];
$lng = $_POST['lng'];
// include db connect class
require_once 'db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO view(id, name, age, rating, info, hours, dresscode, location, tribe, lat, lng, verified) VALUES('', '$name', '$age', '$rating', '$description', '', '', '$location', '', '$lat', '$lng', '0')");
// 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);
}
?>
非常感谢任何帮助!
编辑: 在尝试回答时,我收到了这个错误:
11-15 17:48:55.105: E/AndroidRuntime(8795): FATAL EXCEPTION: main
11-15 17:48:55.105: E/AndroidRuntime(8795): Process: com.spxc.nightclubratings, PID: 8795
11-15 17:48:55.105: E/AndroidRuntime(8795): java.lang.IllegalStateException: Could not execute method of the activity
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.view.View$1.onClick(View.java:3814)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.view.View.performClick(View.java:4424)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.view.View$PerformClick.run(View.java:18383)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.os.Handler.handleCallback(Handler.java:733)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.os.Handler.dispatchMessage(Handler.java:95)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.os.Looper.loop(Looper.java:137)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.app.ActivityThread.main(ActivityThread.java:4998)
11-15 17:48:55.105: E/AndroidRuntime(8795): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 17:48:55.105: E/AndroidRuntime(8795): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 17:48:55.105: E/AndroidRuntime(8795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-15 17:48:55.105: E/AndroidRuntime(8795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-15 17:48:55.105: E/AndroidRuntime(8795): at dalvik.system.NativeStart.main(Native Method)
11-15 17:48:55.105: E/AndroidRuntime(8795): Caused by: java.lang.reflect.InvocationTargetException
11-15 17:48:55.105: E/AndroidRuntime(8795): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 17:48:55.105: E/AndroidRuntime(8795): at java.lang.reflect.Method.invoke(Method.java:515)
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.view.View$1.onClick(View.java:3809)
11-15 17:48:55.105: E/AndroidRuntime(8795): ... 11 more
11-15 17:48:55.105: E/AndroidRuntime(8795): Caused by: java.lang.IllegalArgumentException: locationName == null
11-15 17:48:55.105: E/AndroidRuntime(8795): at android.location.Geocoder.getFromLocationName(Geocoder.java:171)
11-15 17:48:55.105: E/AndroidRuntime(8795): at com.spxc.nightclubratings.AddLocationActivity.addLocation(AddLocationActivity.java:151)
11-15 17:48:55.105: E/AndroidRuntime(8795): ... 14 more
答案 0 :(得分:2)
您无法在doInBackground()
中定义任何UI对象,例如以下代码:
edClubName = (EditText) findViewById(R.id.edClubName);
edAddress = (EditText) findViewById(R.id.edAddress);
edAge = (EditText) findViewById(R.id.edAge);
edDescription = (EditText) findViewById(R.id.edDescription);
但是,您可以在onPreExecute()
这样做:
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AddLocationActivity.this);
pDialog.setMessage("Creating Product..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
edClubName = (EditText) findViewById(R.id.edClubName);
edAddress = (EditText) findViewById(R.id.edAddress);
edAge = (EditText) findViewById(R.id.edAge);
edDescription = (EditText) findViewById(R.id.edDescription);
}
protected String doInBackground(String... args) {
String sClubName = edClubName.getText().toString();
String sAddress = edAddress.getText().toString();
String sAge = edAge.getText().toString();
String sDescription = edDescription.getText().toString();
//complete with the code