2014-10-28 50 views
0

我遇到了与我的应用程序通信的后端问题。我正在尝试创建一个JSON请求,将用户名和密码值传递给服务器。服务器接受这些值并假设返回我映射到用户对象的用户。构建JSON请求的问题

现在,服务器已设置好,以便它返回设置为接收的完全相同的JSON。

我正在使用杰克逊做所有的JSON映射,有没有办法改变我传递过来匹配下面的JSON的JSON?

这里是我送

[ 
    { 
     "name":"username", 
     "value":"hi" 
    }, 
    { 
     "name":"password", 
     "value":"hi" 
    } 
] 

这里的JSON有什么JSON是应该看起来像,当它接收到由服务器

{ 
    "password":"hi", 
    "username":"hi" 
} 

这里是我的用户REST

public static class AuthUser extends 
      AsyncTask<ArrayList<NameValuePair>, Void, User> { 

     public interface AuthUserDelegate { 

      public void getAuthenticatedUser(User user) throws JSONException; 
     } 

     AuthUserDelegate delegate; 
     Context mContext; 

     public AuthUser(Context context) { 

      mContext = context; 
      this.delegate = (AuthUserDelegate) context; 
     } 

     @Override 
     protected User doInBackground(ArrayList<NameValuePair>... params) { 
      ObjectMapper mapper = new ObjectMapper(); // create once, reuse 
      User user = null; 
      String url = ROUTE_USER_AUTH; 
      HttpPost httppost = new HttpPost(url); 
      HttpClient httpclient = new DefaultHttpClient(); 
      String UserJSONResponse = null; 

      try { 

       String jsonString = mapper.writeValueAsString(params[0]); 
       StringEntity m_stringEntity = new StringEntity(jsonString); 


//    UrlEncodedFormEntity m_entity = new UrlEncodedFormEntity(
//      params[0]); 
       httppost.setEntity(m_stringEntity); 
       httppost.addHeader("Content-type", "application/json"); 

       HttpResponse postResponse = httpclient.execute(httppost); 

       UserJSONResponse = EntityUtils.toString(postResponse 
         .getEntity()); 
       user = mapper.readValue(UserJSONResponse, User.class); 

       Log.e("E AUTH USER", "Status code: " 
         + postResponse.getStatusLine().getStatusCode()); 

       Log.e("E AUTH USER", "Auth was sent, Server returned: \n" 
         + UserJSONResponse); 

      } catch (JsonProcessingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (UnsupportedEncodingException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       httppost.abort(); 
       Log.e("E IO EXCEPTION", "Error for URL: " + url, e); 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return user; 
     } 

     @Override 
     protected void onPostExecute(User result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      // User user = new User(); 
      // Log.e("AUTH POST USERNAME", result.getUser_name()); 
      try { 
       delegate.getAuthenticatedUser(result); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 
    } 

的主要活动收集信息和执行的AsyncTask

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.btnSignIn: 
     username = etUsername.getText().toString().trim(); 
     password = etPassword.getText().toString().trim(); 

     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
     nameValuePairs.add(new BasicNameValuePair("username", username)); 
     nameValuePairs.add(new BasicNameValuePair("password", password)); 
     new UserREST.AuthUser(this).execute(nameValuePairs); 

     break; 

    default: 
     break; 
    } 

} 

回答

2

定义自己的JSONObject:

JSONObject input = new JSONObject(); 
input.put("username", "hi"); 
input.put("password", "hi"); 

并执行这样的任务:

new UserREST.AuthUser(this).execute(input); 

任务应该看起来像这样:

public static class AuthUser extends AsyncTask<JSONObject, Void, User> 
{ 
    // [...] 

    @Override 
    protected User doInBackground(JSONObject... params) 
    { 
     User user = null; 
     String url = ROUTE_USER_AUTH; 
     HttpPost httppost = new HttpPost(url); 
     HttpClient httpclient = new DefaultHttpClient(); 
     String UserJSONResponse = null; 

     try 
     { 
      StringEntity m_stringEntity = new StringEntity(params[0].toString()); 
      // [...] 
+0

谢谢。这解决了我的问题。 – 2014-10-28 19:28:08

0

而不是使用NameValuePair使用JSONObject。

public String getJSONAuth(String user, String pass) { 
    JSONObject jo = new JSONObject(); 
    jo.putString("username", user); 
    jo.putString("password", pass); 
    return jo.toString(); 
} 

这会返回您要查找的内容。