안드로이드 앱을 사용하여 데이터베이스 테이블을 채우려고하지만 제출 버튼을 클릭 할 때마다 아무 일도 일어나지 않습니다. 문자열이 JSONObject로 변환 될 수 없다고 말합니다. 원인은 무엇일까요? android app에서 데이터베이스를 채우려고합니까?
이
는 응답은 XML처럼 보인다 오류12-19 02:45:16.464: E/JSON Parser(1508): Error parsing data org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject
자바 코드
// 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(Leave.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("reason", 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
//Toast.makeText(getApplicationContext(), "Successful", Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), "this is my Toast message!!! =)",
// Toast.LENGTH_LONG).show();
// closing this screen
} 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
if (isset($_POST['reason'])) {
$reason = $_POST['reason'];
//$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 leavelist(reason) VALUES('$reason')");
// 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);
URL이 유효한 JSON을 반환하지 않을 수 있습니다. –
이 URL의 결과 데이터를 게시 할 수 있습니까? –