2016-07-11 13 views
1

Ich bin mit dem folgenden Code Wertvariablen an einen Server schreiben:HTTP Post via Android-Java funktioniert nicht

protected String doInBackground(String... params) { 


try{ 

    URL url= new URL(params[0]); 
    HttpURLConnection httpURLConnection= (HttpURLConnection)url.openConnection(); 
    httpURLConnection.setRequestMethod("POST"); 
    httpURLConnection.setDoOutput(true); 
    httpURLConnection.setDoInput(true); 
    OutputStream outputStream = httpURLConnection.getOutputStream(); 
    BufferedWriter bufferedWriter= new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8")); 
    String post_data= URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(params[1], "UTF-8"); 
    post_data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(params[2], "UTF-8"); 
    bufferedWriter.write(post_data); 
    bufferedWriter.flush(); 
    bufferedWriter.close(); 
    outputStream.close(); 

}catch (MalformedURLException e){ 
    e.printStackTrace(); 
}catch (IOException e){ 
    e.printStackTrace(); 
} 
return null; 

}

Hier ist der Asynchron-Task-Aufruf:

BackgroundWorker backgroundWorker= new BackgroundWorker(this); 
backgroundWorker.execute("http://...", "somename", "somesurname"); 

Der Code läuft gut (keine Fehler), aber ich kann keine Daten in meiner Datenbank sehen (.php funktioniert auch korrekt - doppelt überprüft).

Was könnte das Problem hier sein?

Antwort

1

Ich würde vorschlagen, Volley anstelle, hier ist eine gute und einfache Tutorial: http://www.itsalif.info/content/android-volley-tutorial-http-get-post-put

Aber hier ist, wie ich HttpURLConnection verwendet:

public String executePost() { 
    URL url; 
    HttpURLConnection connection = null; 
    try { 
     //Create connection 
     url = new URL(/*URL HERE*/); 

    String urlParameters = "/*THE PARAMS. YOU KNOW THIS ;) */"; 

    connection = (HttpURLConnection) url.openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setRequestProperty("Content-Type", 
      "application/x-www-form-urlencoded"); 

    connection.setRequestProperty("Content-Length", "" + 
      Integer.toString(urlParameters.getBytes().length)); 
    connection.setRequestProperty("Content-Language", "en-US"); 

    connection.setUseCaches(false); 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 

    //Send request 
    DataOutputStream wr = new DataOutputStream(
      connection.getOutputStream()); 
    wr.writeBytes(urlParameters); 
    wr.flush(); 
    wr.close(); 

    //Get Response 
    InputStream is = connection.getInputStream(); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
    String line; 
    StringBuffer response = new StringBuffer(); 
    while ((line = rd.readLine()) != null) { 
     response.append(line); 
     response.append('\r'); 
    } 
    rd.close(); 
    return response.toString(); 

} catch (Exception e) { 

    e.printStackTrace(); 
    return null; 

} finally { 

    if (connection != null) { 
     connection.disconnect(); 
    } 
} 
}