2016-07-16 9 views
1

私はアンドロイドには新しく、今はアンドロイドのHTTPリクエストに関するベストチュートリアルを探すのに疲れています。私はphpServerに、これらのパラメータを送信しようとしているAndroidからhttp投稿リクエストを送信するには?

  • 名= "いくつかの名前"

  • メール= "一部の電子メール"

は、我々はこれを行うことができますMainActivity.javaファイルにありますか?これにはjar(ライブラリ)ファイルが必要ですか? ご意見をお待ちしております。私はこれを試してみました

@Override 
public void onClick(View view) { 
    if(view == btnSubscribe){ 
     HttpURLConnection client = null; 
     try { 
      URL url = new URL("http://www/somewebsite.com/API/SUBSCRIBE"); 
      client = (HttpURLConnection) url.openConnection(); 
      client.setRequestMethod("POST"); 
      client.setRequestProperty("name","test one"); 
      client.setRequestProperty("email","[email protected]"); 
      client.setDoOutput(true); 

      OutputStream outputPost = new BufferedOutputStream(client.getOutputStream()); 
      outputPost.flush(); 
      outputPost.close(); 
      client.setChunkedStreamingMode(0); 

     } catch(MalformedURLException error) { 
      showError("Handles an incorrectly entered UR"); 
     } 
     catch(SocketTimeoutException error) { 
      showError("Handles URL access timeout"); 
     } 
     catch (IOException error) { 
      showError("Handles input and output errors"); 
     }finally { 
      if(client != null) 
      client.disconnect(); 
     } 
    } 
} 
+0

ここでコード???? ???? –

+1

@Sahan [this](http://stackoverflow.com/help/mcve) – Nisarg

+0

@IntelliJAmiya申し訳ありません私のために働いていないので、すべてのコードをクリアしました。今私は新しいプロジェクトに入っています。 – Sahan

答えて

5

私はこのコードを使用しています。このコードを試してください。

public static String httpPostRequest(Context context, String url, String email) { 
     String response = ""; 
     BufferedReader reader = null; 
     HttpURLConnection conn = null; 
     try { 
      LogUtils.d("RequestManager", url + " "); 
      LogUtils.e("data::", " " + data); 
      URL urlObj = new URL(url); 

      conn = (HttpURLConnection) urlObj.openConnection(); 
      conn.setRequestMethod("POST"); 
      conn.setDoOutput(true); 
      OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 

      data += "&" + URLEncoder.encode("Email", "UTF-8") + "=" 
        + URLEncoder.encode(email, "UTF-8"); 


      wr.write(data); 
      wr.flush(); 

      LogUtils.d("post response code", conn.getResponseCode() + " "); 

      int responseCode = conn.getResponseCode(); 



      reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 

      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 

      response = sb.toString(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      LogUtils.d("Error", "error"); 
     } finally { 
      try { 
       reader.close(); 
       if (conn != null) { 
        conn.disconnect(); 
       } 
      } catch (Exception ex) { 
      } 
     } 
     LogUtils.d("RESPONSE POST", response); 
     return response; 
    } 
+0

素晴らしい! – Sahan

+0

は 'data'をどこで定義したかわかりません – lucidbrot

0

まず、あなたが今同じのためのHttpURLConnectionを使用することができますので、もし最もhttpClint廃止されます。

あなたは here

を参照することができますし、ライブラリを使用する場合、あなたはこれらの二つは最高のライブラリです

を使用することができます。

乾杯

関連する問題