2016-04-03 26 views
0

Android uygulamamdan arka uçlara parametreler göndererek ve POST Metodu'ndaki android istemcilerim tarafından gönderilen parametreleri almaya çalışıyorum ancak istemciler olsa bile null parametreleri almaya devam ediyorum null olmayan parametreler gönderiliyor.Java REST API: POST Yöntemi NULL parametreleri alır

Java POST Metodu:

@POST 
@Produces({ "application/json" }) 
@Path("/login") 
public LoginResponse Login(@FormParam("email") String email, @FormParam("password") String password) { 
    LoginResponse response = new LoginResponse(); 

    if(email != null && password != null && email.length() != 0 && password.length() != 0){ 
     //Detect if null or empty 
     //Code 
    } 

    return response; 
} 

Android Müşteri:

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://MY_APP_NAME.appspot.com/user/login"); 

String json = ""; 
JSONObject jsonObject = new JSONObject(); 

try { 
    jsonObject.accumulate("email", "[email protected]"); 
    jsonObject.accumulate("password", "123"); 

    json = jsonObject.toString(); 

    StringEntity se = new StringEntity(json); 
    httppost.setEntity(se); 
    httppost.setHeader("Content-Type", "application/json"); 
    httppost.setHeader("ACCEPT", "application/json"); 
    HttpResponse httpResponse = httpclient.execute(httppost); 
} 
catch(Exception ex) { } 

Ben yöntemin Content-Type inanıyoruz ve istemci yanı aynıdır. Neden Java Backend Metodu'ndan parametreleri almıyorum?

CHECKED:

  • URL'nin doğru ve bağlantı uygulaması tarafından gönderilen
  • Parametreler çalışıyor Biz seni got umut
+0

Sizde Java istemcisindeyken 'LoginResponse' kontrol ettiniz mi? [Post man] 'yı (https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop?hl=tr) kullanın ve parametrelerinizi posta türüyle aynı URL'ye geçirin, ne aldığınızı bize bildirin. Bence arka ucunda hiçbir şey döndürmüyorsun – Kathi

+0

Veriyi geri veriyorum ve vücut verisini x-www-form-urlencoded olarak gönderdiğimde postacıda iyi çalışıyor. Neden olduğu hakkında bir fikrim yok. Bu konuya ilgisiz olduğu için dahil etmedim. Yine de düzenledim. Sorun şu ki, arka uç yönteminde @Kathi – Dinuka

cevap

0

boş değildir, NameValuePair

deneyin
public void postData() { 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.yoursite.com/yourscript.php"); 

    try { 

     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("id", "123")); 
     nameValuePairs.add(new BasicNameValuePair("string", "Hey")); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
     // Catch Protocol Exception 
    } catch (IOException e) { 
     // Catch IOException 
    } 
} 
+0

null parametrelerini alıyorum. – Dinuka

0

Sadece sizin için benzer sorunlarınız varsa, uygulamanızın arka uç sunucusuna ve arka uç yanıtına gönderdiği http isteğini görebileceğiniz ücretsiz bir http denetçisi ve hata ayıklayıcısı olan Fiddler 'u kullanmanızı öneririm.

Şansın en iyisi