2016-03-27 14 views
2

実際には、httpurlconnection getメソッドでパラメータを渡したいと思います。私はアンドロイドで新しいので、私はこれを行うことができません。誰も私にどのようにアンドロイドのパラメータでリクエストを送信するか教えてください...私を助けてください。httpurlconnectionでパラメータを取得するリクエストを送信するにはどうすればよいですか?

public AttemptSignIn(Context context) { 
     this.context = context; 
    } 
    @Override 
    protected String doInBackground(String... args) { 
     // TODO Auto-generated method stub 
     try{ 

      URL url = new URL("http://winktechs.com/windictor/sign_in.php"); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

      conn.setReadTimeout(10000); 
      conn.setConnectTimeout(15000); 
      conn.setRequestMethod("GET"); 
      conn.setDoInput(true); 
      //conn.setDoOutput(true); 

      JSONObject jsonObj = new JSONObject(); 
      jsonObj.put("email", email.getText().toString()); 
      jsonObj.put("password", password.getText().toString()); 

      JSONArray jsonArray = new JSONArray(); 
      jsonArray.put(jsonObj); 


      conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); 


      conn.connect(); 
      int responseCode = conn.getResponseCode(); 

      Log.d("response", responseCode + ""); 

      if (responseCode==200) { 
       inputStream = new BufferedInputStream(conn.getInputStream()); 
       String response = convertInputStreamToString(inputStream); 
       // parseResult(response); 
       result = 1; // Successful 
       flag = true; 
      } 
      else 
      { 
       flag = false; 

      } 

     } 
     catch (JSONException ej) 
     { 
      flag = false; 
      ej.printStackTrace(); 
     } 

     catch (Exception e) 
     { 

      e.printStackTrace(); 
     } 

     return null; 
    } 
    protected void onPostExecute(String file_url) { 
     if(flag==true) 
     { 
      Toast.makeText(context, "Logged In Successfully !", Toast.LENGTH_LONG).show(); 
      Intent intent = new Intent(SignIn.this,MainActivity.class); 
      startActivity(intent); 
      finish(); 
     } 
     else 
     { 
      //Toast.makeText(context, Res.toString(),Toast.LENGTH_LONG).show(); 
     } 

    } 

} 

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

     /* Close Stream */ 
    if(null!=inputStream){ 
     inputStream.close(); 
    } 
    return result; 
} 
+1

単純なロジック:HTTP GETはパラメータをPOSTするための適切な方法ではありません... – Selvin

+0

サーバーからデータを読み取る方法は? POSTでこれを行うことはできますか? –

+0

あなたはあなたのサーバーにhttp://winktechs.com/windictor/sign_in.php?id=のようなURLでデータを投稿し、GET –

答えて

-3

それは、少なくとも、代わりにPOSTメソッドを使用して、HTTP経由で電子メール、パスワード、電話番号などの機密データを送信するとき、GETを使用して回避するために、セキュリティ上の理由から推奨されます。
HTTP POSTを使用: - jsonobjectをJsonStringに変換します。 - OutputStreamを使用して、パラメータを投稿に添付します。

JsonString sParams = jsonobject.toString(); 
// write parameter to post 
OutputStream urlParam = httppost.getOutputStream(); 
// use buffer 
BufferedWriter buff = new BufferedWriter (new OutputStreamWriter (urlParam)); 
buff.write (sParams); 
buff.flush(); 
buff.close(); 
// close the Stream 
UrlParam.close(); 
関連する問題