gpt4 book ai didi

java - 发出结构化 JSON 请求

转载 作者:行者123 更新时间:2023-12-02 05:19:26 25 4
gpt4 key购买 nike

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

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

我正在使用 Jackson 进行所有 JSON 映射,有没有办法更改我传递的 JSON 以匹配下面的 JSON?

这是我发送的 JSON

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

这是服务器接收到 JSON 时的样子

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

这是我的用户休息

    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();
}

}
}

收集信息和执行异步任务的主要 Activity

    @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;
}

}

最佳答案

定义您自己的 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());
// [...]

关于java - 发出结构化 JSON 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26614593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com