2014-12-19 4 views
0

안드로이드 앱을 사용하여 데이터베이스 테이블을 채우려고하지만 제출 버튼을 클릭 할 때마다 아무 일도 일어나지 않습니다. 문자열이 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); 
+2

URL이 유효한 JSON을 반환하지 않을 수 있습니다. –

+0

이 URL의 결과 데이터를 게시 할 수 있습니까? –

답변

-1

입니다. 이 도움이 될 것입니다

header('Content-Type: application/json'); 

희망을 : 당신이 응답이 정말 JSON 있는지 확인하기 위해 브라우저를 사용하려고하거나 다음과 같이 당신의 PHP 파일에 JSON으로 반환 유형을 설정하려고 할 수 있다면 그것은 좋은 것입니다.

+0

그는 json_encode ($ response); 그것은 그가 json 만 에코를 울리려고한다는 것을 의미합니다 –

+0

하지만 오류 메시지를 읽으면 "데이터를 파싱하는 중 오류가 발생했습니다. org.json.JSONException : 값 Dara

관련 문제