2017-07-31 2 views
1

Je souhaite envoyer une donnée POST RAW de {"userid": "userid","pass":"1222"} en tant que nom d'utilisateur et mot de passe. J'ai une mise en page consistant en un nom d'utilisateur et un mot de passe qui sera récupéré en tant que nom d'utilisateur et mot de passe. J'ai besoin d'aide pour essayer ce pour moderniserAndroid. Comment envoyer des données brutes en utilisant un retrofit ou de l'aide en utilisant asynctask

// Triggers when LOGIN Button clicked 
    public void checkLogin(View arg0) { 

      // Initialize AsyncLogin() class with userid and password 
      new REST().execute(); 

    } 



    public class REST extends AsyncTask<Void, Void, Void> { 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // The Username & Password 
      final EditText usr = (EditText) findViewById(R.id.username); 
      userid = (String) usr.getText().toString(); 
      final EditText pw = (EditText) findViewById(R.id.password); 
      password = (String) pw.getText().toString(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      HttpURLConnection urlConnection=null; 
      String json = null; 

      // ----------------------- 

      try { 
       HttpResponse response; 
       JSONObject jsonObject = new JSONObject(); 
       jsonObject.accumulate("username", usr); 
       jsonObject.accumulate("password", password); 
       json = jsonObject.toString(); 
       HttpClient httpClient = new DefaultHttpClient(); 
       HttpPost httpPost = new HttpPost("http://mark.journeytech.com.ph/mobile_api/authentication.php"); 
       httpPost.setEntity(new StringEntity(json, "UTF-8")); 
       httpPost.setHeader("Content-Type", "application/json"); 
       httpPost.setHeader("Accept-Encoding", "application/json"); 
       httpPost.setHeader("Accept-Language", "en-US"); 
       response = httpClient.execute(httpPost); 
       String sresponse = response.getEntity().toString(); 
       Log.w("QueingSystem", sresponse); 
       Log.w("QueingSystem", EntityUtils.toString(response.getEntity())); 
      } 
      catch (Exception e) { 
       Log.d("InputStream", e.getLocalizedMessage()); 

      } finally { 
// nothing to do here 

      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      Toast.makeText(getApplicationContext(), email + " "+ password, Toast.LENGTH_SHORT).show(); 
      if (result != null) { 
       // do something 
      } else { 
       // error occured 
      } 
     } 

vous plaît toute aide parce que je cherchais beaucoup et n'atteins rien

+0

Cocher cette https://code.tutsplus.com/tutorials/sending-data-with-retrofit-2-http-client-for-android--cms-27845 –

+0

utilisant Asyntask vous avez fait ou pas? –

+0

Salut Sunil, cela peut être fait. Pls. voir https://stackoverflow.com/questions/45428585/postman-is-working-cannot-send-raw-data-on-android/45428742?noredirect=1#comment77819608_45428742 –

Répondre

0

Vous devez suivre les étapes suivantes:

  1. interface API réseau
public interface NetworkAPI { 
    @GET("authentication.php") 
    @Headers({"Content-Type:application/json; charset=UTF-8"}) 
    Call<JsonElement> loginRequest(@Body LoginRequest body); 
} 
  1. Login classe modèle
public class LoginRequest { 
    String userid; 
    String password; 
    public LoginRequest(String userid, String password) { 
     this. userid = userid; 
     this. pass = pass; 
    } 
} 
  1. appel Rénovation dans votre activité

Chaîne baseUrl = "http://mark.journeytech.com.ph/mobile_api/";

RéseauAPI networkAPI;

public void loginRequest(){ 

    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl(baseUrl) 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 

    networkAPI = retrofit.create(NetworkAPI.class); 

    LoginRequest loginRequest = new LoginRequest(yourusername,yourpassword); 

    Call<JsonElement> call = networkAPI.loginRequest(loginRequest); 

    call.enqueue(new Callback<JsonElement>() { 
     @Override 
     public void onResponse(Call<JsonElement> call, Response<JsonElement> response) { 
      // success response 

     } 

     @Override 
     public void onFailure(Call<JsonElement> call, Throwable t) { 
      // failure response 
     } 
    }); 
} 
+0

Salut Dory, merci pour ce merveilleux commentaire. Cependant, lorsque je mets les informations d'identification correctes comme nom d'utilisateur: ssrci et mot de passe: 1222. Je n'obtiens pas ce statut { ":" Connectez-vous au succès!", " ucsi_num ":" MARK12302010035824 ", " table_client ":" CRM_ACTIVE ", " markutype ": 3 }' –

+0

partagez votre gradle pour la rénovation. – Dory

0

Première: vous devez créer votre interface api

 public interface Api 
    { 
    @Headers({"Accept: application/json"}) 
    @FormUrlEncoded 
    @POST("authentication.php") 
    Call<Void> Login(@Field("[email]") String email, 
       @Field("[password]") String password); 
    } 

Deuxièmement: Dans votre activité, vous devriez appeler votre fonction

void Login(String email, final String password) 
    { 
    Retrofit retrofit = new Retrofit.Builder() 
      .baseUrl("http://mark.journeytech.com.ph/mobile_api/") 
      .addConverterFactory(GsonConverterFactory.create()) 
      .build(); 
    Apiservice = retrofit.create(Api.class); 
    Call<Void> call = service.Login(email, password); 
    call.enqueue(new Callback<Void>() 
    { 
     @Override 
     public void onResponse(Call<Void> call, Response<Void> response) 
     { 
       if (response.isSuccess()) 
       { 

       } 
       else 
       { 
       } 
      } 
     @Override 
     public void onFailure(Call<Void> call, Throwable t) 
     { 
     } 
    }); 
} 
+0

Salut Mina, qu'est-ce que Apiservice et devrais-je supprimer mon Asynctask? Aussi, je mets BASE_URL = "http://mark.journeytech.com.ph". pls. être guidé. Merci –

+0

oui n'utilisez pas votre asyncTask. et le BASE_URL = http://mark.journeytech.com.ph/mobile_api/ et dans votre interface api mettez votre_api_name = authentication.php. je vais éditer ma réponse –

+0

Salut Mina, merci pour ce commentaire. Btw, qu'y a-t-il dans Apiservice? –