2016-08-19 15 views
0

私はアンドロイドアプリにHTTPUrlConnectionを投稿し、ウェブサービスからデータを取得しています。現時点で私はすでにデータを取得して表示することに成功しましたが、今はmysqlデータベースに追加するためにデータを投稿する必要があります。これはどのように達成できますか?Android - HTTPUrlConnectionサーバーへのPOSTデータ

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; 

    } 
} 

答えて

0

あなたのMainActivityクラス内で次のメソッドを追加することができます:

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); 
} 

はそれが役に立てば幸い

は、ここに私のコードです!

+0

申し訳ありませんが、私が送る値はこの行にありますか? urlConnection.setRequestProperty( "Content-Type"、contentType); –

+0

'contentType'は、 'application/json'のように、このメソッドでパラメータとして受け取る変数です。 'text/html'など –

関連する問題