我正在使用以下代码进行httpPost调用,但是当我尝试在chrome扩展中的"简单休息客户端"中提供以下参数时,它返回了400个错误的请求它可以正常工作任何人指导我在这里做了什么错误?
简单休息客户端我输入以下内容:
URL:http://jon2012.com/api/register
方法:POST标题:没有标题,因为它们不是必需的数据:{"email":"test@example.com","first_name":"名称"}
Android代码:
HttpClient client = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit
HttpResponse response;
JSONObject json = new JSONObject();
try{
HttpPost post = new HttpPost(url);
json.put("email", email);
json.put("first_name", name);
StringEntity se = new StringEntity( "JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
response = client.execute(post);
/*Checking response */
/*if(response!=null){
InputStream in = response.getEntity().getContent(); //Get the data in the entity
*/
int statusCode = response.getStatusLine().getStatusCode();
}
catch(Exception e){
e.printStackTrace();
// createDialog("Error", "Cannot Estabilish Connection");
}
Run Code Online (Sandbox Code Playgroud)
任何帮助都会得到满足
我必须向Web服务发送请求,以使用用户名和密码对用户进行身份验证.
我对以下帖子请求有疑问:
public String postTest(String action, ConnectionParametrData [] parameters) {
Uri.Builder builder = new Uri.Builder().scheme(scheme).authority(authority).path(action);
uri = builder.build();
BufferedReader in = null;
String ans = null;
HttpPost request = new HttpPost(uri.toString());
HttpClient defaultClient = new DefaultHttpClient();
try {
request.setHeader("Content-Type", "application/x-www-form-urlencoded");
request.setEntity(new UrlEncodedFormEntity(getValuePairs(parameters)));
HttpResponse response = defaultClient.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"), 8192);
StringBuffer sb = new StringBuffer("");
String line = "";
String newLine = System.getProperty("line.separator");
while((line = in.readLine()) != null) {
sb.append(line + newLine);
}
ans = sb.toString();
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试连接到groovehark API,这是http请求
POST URL
http://api.grooveshark.com/ws3.php?sig=f699614eba23b4b528cb830305a9fc77
POST payload
{"method":'addUserFavoriteSong",'parameters":{"songID":30547543},"header":
{"wsKey":'key","sessionID":'df8fec35811a6b240808563d9f72fa2'}}
Run Code Online (Sandbox Code Playgroud)
我的问题是如何通过Java发送此请求?