gpt4 book ai didi

java - 使用名称值对将 Json POST 发送到服务器

转载 作者:太空狗 更新时间:2023-10-29 16:19:11 25 4
gpt4 key购买 nike

我正在通过 url 向服务器发送 Json Post 请求:http://www.xyz.com/login

请求结构:

{"requestdata":{"password":"abc","devicetype":"phone","username":"amrit@pqr.com","locale":"in"},"requestcode":10}

代码快照:

主要 Activity :

    // Building post parameters
// key and value pair
List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
nameValuePair.add(new BasicNameValuePair("requestcode", "10"));
nameValuePair.add(new BasicNameValuePair("devicetype", "phone"));
nameValuePair.add(new BasicNameValuePair("locale", "in"));
nameValuePair.add(new BasicNameValuePair("username", "amrit@pqr.com"));
nameValuePair.add(new BasicNameValuePair("password", "abc"));

RestPost post = new RestPost(loginUrl, nameValuePair);
String Response = post.postData();
Log.i("Response:", Response);

RestPost 类

    import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;

public class RestPost {
String url;
List<NameValuePair> nameValuePairs;

public RestPost(String str, List<NameValuePair> params) {
this.url = str;
this.nameValuePairs = params;
}

public String postData() {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(this.url);
StringBuilder builder = new StringBuilder();

try {
httppost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
Log.d("RestClient", "Status Code : " + statusCode);

HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}

} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
return builder.toString();
}
}

但我没有得到适当的响应,任何人都可以帮助我发送适当的格式以获得服务器响应。提前致谢。

我得到的响应:

{"error":{"resultCode":"400","status":"Invalid Request format"}}

最佳答案

您当前正在以

的形式发送 JSON
{
"requestcode": "10",
"devicetype": "phone",
"locale": "in",
"username": "amrit@pqr.com",
"password": "abc"
}

这不是服务器要求的形式。尝试创建一个要发送的 JSON 字符串。然后使用:

httppost.setEntity(new StringEntity(jsonString, "UTF8"));
httppost.setHeader("Content-type", "application/json");

将字符串发送到服务器。

关于java - 使用名称值对将 Json POST 发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19682609/

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