2016-07-16 8 views
1

저는 안드로이드를 처음 접했고 이제는 안드로이드 HTTP 요청에 대한 최고의 자습서를 찾는 것에 지쳤습니다. 내가 phpServer에 이러한 매개 변수를 보내려고안드로이드에서 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 Go [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

를 참조 할 수 있습니다 당신이 라이브러리를 사용하려는 경우 당신은

최고의 라이브러리입니다 사용할 수 있습니다.

건배

관련 문제