2016-03-27 7 views
2

Eigentlich möchte ich Parameter in httpurlconnection erhalten Methode übergeben. Ich bin nicht in der Lage, dies zu tun, da ich neu in Android bin. Kann mir bitte jemand sagen, wie man senden Anfrage mit Parametern in Android ... bitte hilf mir. mit GET zu vermeiden, wenn das Senden sensible Daten wie E-Mail, Passwort, Telefonnummer über http, Verwendung POST-Methode anstelle mindestensWie senden Sie Anfrage mit Parametern in 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

einfache Logik: HTTP GET ist keine richtige Methode Parameter für die Buchung ... – Selvin

+0

so, wie die Daten vom Server zu lesen? Können wir das mit POST machen? –

+0

Sie müssen Daten auf Ihrem Server in der URL wie http://winktechs.com/windictor/sign_in.php?id= veröffentlichen und lesen Sie sie mit GET –

Antwort

-3

Es wird aus Sicherheitsgründen empfohlen.
Verwenden Sie HTTP POST: - konvertieren Sie Ihr JsonObject in JsonString - verwenden Sie OutputStream, um die Parameter an den Post z.

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(); 
Verwandte Themen