2015-05-19 70 views
0

我试图从我的应用程序插入数据到数据库中。我可以获取数据,但是当我尝试将变量传递到我的服务器时,出现“必填字段丢失”错误。变量不被发送到服务器

我以前用不同的应用程序完成此操作,但是之前我在我的网站上安装了SSL。有没有机会SSL可以阻止变量。

我试着保持代码尽可能简单的测试目的,但我只是无法弄清楚。我已经重做了几个教程,以确保我没有犯错误,但显然我在某个地方出错了。任何帮助非常感谢!

class CreateNewProduct extends AsyncTask<String, String, String> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(NewProductActivity.this); 
      pDialog.setMessage("Creating Product.."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     protected String doInBackground(String... args) { 

      List<NameValuePair> params = new ArrayList<NameValuePair>(); 
      params.add(new BasicNameValuePair("name", "name")); 
      params.add(new BasicNameValuePair("price", "2")); 
      params.add(new BasicNameValuePair("imgurl", "imgurl")); 

      JSONObject json = jsonParser.makeHttpRequest("http://myserver.com", 
        "POST", params); 

      Log.d("Create Response", json.toString()); 

      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        Intent i = new Intent(getApplicationContext(), AllProductsActivity.class); 
        startActivity(i); 

        finish(); 
       } else { 
Log.d("TEST", "Failed to create product"); 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return null; 
     } 

     protected void onPostExecute(String file_url) { 
      pDialog.dismiss(); 
     } 

    } 

PHP代码:

<?php 

$response = array(); 

if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['imgurl'])) { 

    $name = "joey"; 
    $price = "3"; 
    $imgurl = "blowey"; 

    require_once __DIR__ . '/db_connect.php'; 

    $db = new DB_CONNECT(); 

    $result = mysqli_query("INSERT INTO products(name, price, imgurl) VALUES('$name', '$price', '$imgurl')"); 

    if ($result) { 
     $response["success"] = 1; 
     $response["message"] = "Product successfully created."; 
     echo json_encode($response); 
    } else { 
     $response["success"] = 0; 
     $response["message"] = "Oops! An error occurred."; 
     echo json_encode($response); 
    } 
} else { 
    $response["success"] = 0; 
    $response["message"] = "Required field(s) is missing"; 
    echo json_encode($response); 
} 
?> 

JSON解析器:

public class JSONParser { 

    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 

    // constructor 
    public JSONParser() { 

    } 

    // function get json from url 
    // by making HTTP POST or GET mehtod 
    public JSONObject makeHttpRequest(String url, String method, 
             List<NameValuePair> params) { 

     // Making HTTP request 
     try { 

      // check for request method 
      if(method == "POST"){ 
       // request method is POST 
       // defaultHttpClient 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost(url); 
       httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8")); 

       HttpResponse httpResponse = httpClient.execute(httpPost); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 

      }else if(method == "GET"){ 
       // request method is GET 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       String paramString = URLEncodedUtils.format(params, "utf-8"); 
       url += "?" + paramString; 
       HttpGet httpGet = new HttpGet(url); 

       HttpResponse httpResponse = httpClient.execute(httpGet); 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       is = httpEntity.getContent(); 
      } 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "iso-8859-1"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 

     // return JSON String 
     return jObj; 

    } 
} 
+0

你确定你的insert语句正确吗? 'INSERT INTO products SET name =?,price =?,description =? ' - 通常'插入产品**值**(...)' - 但它可能是数据库特定的mySql? –

+0

'我得到“必填字段丢失”错误。不可能,因为你的代码不读取echo()的。 – greenapps

+0

@greenapps对不起!我正在尝试一些不同的事情,并且意外地发布了错误的代码。我已经更新了代码。此代码给我“必填字段..”错误。 – Jack1204

回答

1

尝试更换:

post.setEntity(new UrlEncodedFormEntity(dataToSend)); 

post.setRequestBody(dataToSend); 
+0

对不起,我意外地从一个不同的教程发布了错误的代码块我是尝试,现在已更新 – Jack1204

+0

*我得到“必填字段丢失”错误*您在哪里看到此消息? – Alex

+0

在我的logcat中,JSON响应 - Log.d(“Create Response”,json。 toString()); – Jack1204