2016-08-19 7 views
0

Ich habe eine HTTPUrlConnection in meiner Android-App, um Daten von einem Webservice zu posten und zu erhalten. Im Moment habe ich bereits erreicht, die Daten zu bekommen und anzuzeigen, aber jetzt muss ich Daten posten, um sie zu meiner MySQL-Datenbank hinzuzufügen. Wie kann dies erreicht werden?Android - HTTPUrlConnection POST-Daten zum Server

Hier ist mein Code:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     new HTTPAsyncTask().execute("http://192.168.0.16/MyDayFiles/borrar.php"); 
    } 

    private class HTTPAsyncTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... urls) { 

      try { // params comes from the execute() call: params[0] is the url. 
       return HttpGet(urls[0]); 
      } catch (IOException e) { 
       return "Unable to retrieve web page. URL may be invalid."; 
      } 
     } 

     @Override // onPostExecute displays the results of the AsyncTask. 
     protected void onPostExecute(String result) { 
      //SHOW RESPONSE 
     } 
    } 

    private String HttpGet(String myUrl) throws IOException { 
     InputStream inputStream = null; 
     String result = ""; 

     URL url = new URL(myUrl); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // create HttpURLConnection 
     conn.connect(); // make GET request to the given URL 
     inputStream = conn.getInputStream(); // receive response as inputStream 

     if(inputStream != null) { // convert inputstream to string 
      result = convertInputStreamToString(inputStream); 
     }else { 
      result = "Hubo un error."; 
     } 
     return result; 
    } 

    private static String convertInputStreamToString(InputStream inputStream) throws IOException{ 
     BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
     String line = ""; 
     String result = ""; 
     while((line = bufferedReader.readLine()) != null) { 
      result += line; 
     } 
     inputStream.close(); 
     return result; 

    } 
} 

Antwort

0

Sie können die folgende Methode in Ihrem MainActivity Klasse hinzufügen:

public String HttpPost(String myUrl, String contentType, byte[] data) throws IOException 
{ 
    URL url = new URL(myURL); 
    HttpUrlConnection urlConnection = (HttpUrlConnection)url.openConnection(); 

    urlConnection.setRequestMethod("POST"); 
    urlConnection.setDoOutput(true); 
    urlConnection.setDoInput(true); 

    //I suggest you to receive an ArrayList of Pair<String, String> or 
    //somethig and then iterate it setting all request header properties that 
    //you need. 
    urlConnection.setRequestProperty("Content-Type", contentType); 

    OutputStream outputStream = urlConnection.getOutputStream(); 
    outputStream.write(data); 

    //You can handle HTTP errors based on this code 
    int responseCode = urlConnection.getResponseCode(); 
    InputStream inputStream = urlConnection.getErrorStream(); 

    if(inputStream == null) //If inputStream is null here, no error has occured. 
     inputStream = urlConnection.getInputStream(); 

    return convertInputStreamToString(inputStream); 
} 

Hoffe, es hilft!

+0

es tut mir leid, der Wert, den ich senden werde, ist in dieser Zeile? urlConnection.setRequestProperty ("Inhaltstyp", contentType); –

+0

'contentType' ist eine Variable, die Sie als Parameter in dieser Methode erhalten, wie 'application/json'; 'text/html' usw. –

Verwandte Themen