2017-01-22 22 views
1

下面是我用它來發送數據到PHP無法從Android的數據發送到PHP JSON

public String createUser(String url,String method,List<Pair<String, String>> params){ 
     //Making Http request 
     HttpURLConnection httpURLConnection = null; 
     StringBuffer response = null; 
     String lineEnd = "\r\n"; 

     try{ 
      if(method.equals("POST")){ 
       URL urlPost = new URL(url); 
       httpURLConnection = (HttpURLConnection) urlPost.openConnection(); 
       httpURLConnection.setDoOutput(true); //defaults request method to POST 
       httpURLConnection.setDoInput(true); //allow input to this HttpURLConnection 
       httpURLConnection.setUseCaches(false); 
       httpURLConnection.setRequestMethod("POST"); 
       //httpURLConnection.setRequestProperty("Content-Type","application/json"); 
       //httpURLConnection.setRequestProperty("Host", "192.168.0.101"); 
       httpURLConnection.connect(); 
       DataOutputStream wr = new DataOutputStream(httpURLConnection.getOutputStream()); 
       wr.writeBytes(params.toString()); 
       //wr.writeBytes("user_email="+userEmailText); 
       //wr.writeBytes(lineEnd); 
       wr.flush(); //flush the stream when we're finished writing to make sure all bytes get to their destination 
       wr.close(); 
       InputStream is = httpURLConnection.getInputStream(); 
       BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
       String line; 
       response = new StringBuffer(); 
       while((line = rd.readLine()) != null) { 
        response.append(line); 
        response.append('\r'); 
       } 
      } 

     } catch (ProtocolException e) { 
      e.printStackTrace(); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return response.toString(); 

    } 

在我的AsyncTask類中的方法:

params.add(new Pair<>("user_name", userNameText)); 
params.add(new Pair<>("user_email", userEmailText)); 
    HttpHandler sh = new HttpHandler(); 
      String jsonStrUserCreation = sh.createUser(url,"POST",params); 
      System.out.println("userNameText: " + userNameText); 
      System.out.println("userEmailText: " + userEmailText); 
      Log.e(TAG, "Response from userCreationURL: " + jsonStrUserCreation); 
      try{ 
       JSONObject jsonObj = new JSONObject(jsonStrUserCreation); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

下面是我的PHP代碼:

<?php 

    require_once 'connecttodb.php'; 
    $db = new DB(); 
    $con = $db->db_connect(); 

    if(isset($_POST['user_name']) && isset($_POST['user_email'])){ 
     $user_name = $_POST['user_name']; 
     $user_email = $_POST['user_email']; 
     $sql = "INSERT INTO user_details(user_name,user_email) VALUES('$user_name','$user_email')"; 
     $run = mysqli_query($con,$sql); 
     if($run){ 
      $response["success"] = 1; 
      $response["message"] = "Account successfully created"; 
      echo json_encode($response); 
     }else{ 
      $response["success"] = 0; 
      $response["message"] = "Account failed to be created"; 
      echo json_encode($response); 
     } 
    }else{ 
      $response["success"] = 2; 
      $response["message"] = "Failed to run inner code"; 
      echo json_encode($response); 
    } 

當我傳入user_name和user_email的值時,腳本始終返回「無法運行內部代碼」。

+0

爲什麼你認爲腳本reurning該字符串? – greenapps

+0

@greenapps我創建了一個HTML表單來測試腳本,並設法插入到數據庫中,這意味着我不認爲它的PHP腳本的問題 – kylas

+0

爲什麼你將''\ r''附加到你的行。這不是''\ n'嗎? – peter

回答

0

實測值如以下的溶液。請確保您的字符串格式如下

String urlParameters = "user_name="+userNameText+"&user_email="+userEmailText; 

然後如下調用它:

sh.createUser(url,urlParameters); 

你會看到神奇。

+0

查詢參數必須進行編碼。 'URLEncoder.encode(userEmailText, 'UTF-8')' – peter