Java 使用名称值对将 Json POST 发送到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19682609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending Json POST to server using name value pair
提问by Amrit Pal Singh
I am sending Json Post request to server to through url: http://www.xyz.com/login
我通过 url 向服务器发送 Json Post 请求:http: //www.xyz.com/login
request structure:
请求结构:
{"requestdata":{"password":"abc","devicetype":"phone","username":"[email protected]","locale":"in"},"requestcode":10}
Code Snapshot:
代码快照:
MainActivity:
主要活动:
// 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", "[email protected]"));
nameValuePair.add(new BasicNameValuePair("password", "abc"));
RestPost post = new RestPost(loginUrl, nameValuePair);
String Response = post.postData();
Log.i("Response:", Response);
RestPost class
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();
}
}
But I'm not getting appropriate response, could anyone help me in sending appropriate format for getting server response. Thanks in advance.
但是我没有得到适当的响应,任何人都可以帮助我发送适当的格式以获取服务器响应。提前致谢。
Response which I'm getting:
我得到的回应:
{"error":{"resultCode":"400","status":"Invalid Request format"}}
采纳答案by MungoRae
You are currently sending the JSON in the form of
您当前正在以以下形式发送 JSON
{
"requestcode": "10",
"devicetype": "phone",
"locale": "in",
"username": "[email protected]",
"password": "abc"
}
Which isn't the form the server is asking for. Try creating a string of the JSON you want to send. Then use:
这不是服务器要求的形式。尝试创建要发送的 JSON 字符串。然后使用:
httppost.setEntity(new StringEntity(jsonString, "UTF8"));
httppost.setHeader("Content-type", "application/json");
To send the string to the server.
将字符串发送到服务器。
回答by SSS
I'm using AndroidHttpClient to post the request
我正在使用 AndroidHttpClient 发布请求
AndroidHttpClient httpClient = AndroidHttpClient.newInstance("User Agent");
URL urlObj = new URL(url);
HttpHost host = new HttpHost(urlObj.getHost(), urlObj.getPort(), urlObj.getProtocol());
AuthScope scope = new AuthScope(urlObj.getHost(), urlObj.getPort());
HttpContext credContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost (url);
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairsArrayList));
// Execute post request and get http response
HttpResponse httpResponse = httpClient.execute(host, httpPost, credContext);
httpClient.close();
This works perfectly for me.
这对我来说非常有效。
回答by abc
AndroidHttpClient http = AndroidHttpClient.new Instance("hai");