Android请求中的Post参数
我正在做一个Android应用程序,但在针对自己的服务器执行请求时遇到了问题。我使用Play Framework制作了服务器,并从Json获取参数:Android请求中的Post参数,android,post,parameters,request,Android,Post,Parameters,Request,我正在做一个Android应用程序,但在针对自己的服务器执行请求时遇到了问题。我使用Play Framework制作了服务器,并从Json获取参数: response.setContentTypeIfNotSet("application/json; charset=utf-8"); JsonParser jsonParser = new JsonParser(); JsonElement jsonElement = jsonParser.parse(getBody(re
response.setContentTypeIfNotSet("application/json; charset=utf-8");
JsonParser jsonParser = new JsonParser();
JsonElement jsonElement = jsonParser.parse(getBody(request.body));
Long id =jsonElement.getAsJsonObject().get("id").getAsLong();
当我对服务器发出GET请求时,一切正常。但是,当我发出POST请求时,我的服务器会返回一个未知错误,关于JSON格式错误或无法找到元素的信息
私有ArrayList NameValuePair>params
私有ArrayList NameValuePair>标题
个案职位:
HttpPost postRequest = new HttpPost(host);
// Add headers
for(NameValuePair h : headers)
{
postRequest.addHeader(h.getName(), h.getValue());
}
if(!params.isEmpty())
{
postRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(postRequest, host);
break;
我试图处理请求的参数,但失败了:
如果(!params.isEmpty())
{HttpParams HttpParams=新的BasicHttpParams() 还有不同的错误,这取决于我的请求。它们都是“play.exceptions.JavaExecutionException”:
- 'com.google.gson.stream.MalformedJsonException'
- '这不是JSON对象'
- '找到预期对象:“id”'
我希望有人能帮助我。这里有一个发送HTTP帖子的简单方法
HttpPost httppost = new HttpPost("Your URL here");
httppost.setEntity(new StringEntity(paramsJson));
httppost.addHeader("content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
您最好直接使用JSON字符串,而不是在这里解析它。希望它有帮助。这里是发送HTTP帖子的简单方法
HttpPost httppost = new HttpPost("Your URL here");
httppost.setEntity(new StringEntity(paramsJson));
httppost.addHeader("content-type", "application/json");
HttpResponse response = httpclient.execute(httppost);
Try this,It may help u
public void executeHttpPost(String string) throws Exception
{
//This method for HttpConnection
try
{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("URL");
List<NameValuePair> value=new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("Name",string));
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);
request.setEntity(entity);
client.execute(request);
System.out.println("after sending :"+request.toString());
}
catch(Exception e) {System.out.println("Exp="+e);
}
}
你最好直接使用JSON字符串,而不是在这里解析它。希望它有帮助
Try this,It may help u
public void executeHttpPost(String string) throws Exception
{
//This method for HttpConnection
try
{
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost("URL");
List<NameValuePair> value=new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("Name",string));
UrlEncodedFormEntity entity=new UrlEncodedFormEntity(value);
request.setEntity(entity);
client.execute(request);
System.out.println("after sending :"+request.toString());
}
catch(Exception e) {System.out.println("Exp="+e);
}
}
public void executehttpost(字符串)引发异常
{
//此方法适用于HttpConnection
尝试
{
HttpClient=new DefaultHttpClient();
HttpPost请求=新的HttpPost(“URL”);
列表值=新的ArrayList();
add(新的BasicNameValuePair(“名称”,字符串));
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(值);
请求。设置实体(实体);
执行(请求);
System.out.println(“发送后:+request.toString());
}
catch(异常e){System.out.println(“Exp=”+e);
}
}
试试这个,它可能会对你有所帮助
public void executehttpost(字符串)引发异常
{
//此方法适用于HttpConnection
尝试
{
HttpClient=new DefaultHttpClient();
HttpPost请求=新的HttpPost(“URL”);
列表值=新的ArrayList();
add(新的BasicNameValuePair(“名称”,字符串));
UrlEncodedFormEntity实体=新的UrlEncodedFormEntity(值);
请求。设置实体(实体);
执行(请求);
System.out.println(“发送后:+request.toString());
}
catch(异常e){System.out.println(“Exp=”+e);
}
}
这正是我所做的,因为在我的代码参数上有一个列表。显然,这对我来说不起作用:非常感谢anywhere=)E/Error用户(371):错误500::'play.exceptions.JavaExecutionException',01-22 10:43:06.582:E/Error用户(371):消息:com.google.gson.stream.MalformedJsonException:使用jsonReader.setLenient(true)若要在idUser1=57&idUser2=65 E/Error用户(371)附近接受格式错误的JSON,请执行以下操作:'E/Error用户(371):错误500:'play.exceptions.JavaExecutionException',E/Error用户(371):消息:'这不是JSON对象。'E/Error用户(371):错误500:'play.exceptions.JavaExecutionException',/错误用户(371):消息:'Expecting Object Founded:'name'很抱歉我的演讲不好。什么JSON?我有一个NameValuePair的ArrayList,我唯一能做的就是postRequest.setEntity(新的UrlEncodedFormEntity(params,HTTP.UTF_8));我在Android应用程序中不做任何JSON。(错误来自服务器)。这正是我所做的,因为在我的代码参数上有一个列表。显然,这对我来说不起作用:s非常感谢anywhere=)E/Error用户(371):Error 500::'play.exceptions.JavaExecutionException',01-22 10:43:06.582:E/Error用户(371):message:com.google.gson.stream.MalformedJsonException:使用jsonReader.setLenient(true)若要在idUser1=57&idUser2=65 E/Error用户(371)附近接受格式错误的JSON,请执行以下操作:'E/Error用户(371):错误500:'play.exceptions.JavaExecutionException',E/Error用户(371):消息:'这不是JSON对象。'E/Error用户(371):错误500:'play.exceptions.JavaExecutionException',/错误用户(371):消息:'Expecting Object Founded:'name'很抱歉我的演讲不好。什么JSON?我有一个NameValuePair的ArrayList,我唯一能做的就是postRequest.setEntity(新的UrlEncodedFormEntity(params,HTTP.UTF_8));我在Android应用程序中不做任何JSON。(错误来自服务器)。但我有一个列表参数用于添加到请求中。如果我以字符串格式创建自己的JSON,我必须手动执行此操作?谢谢大家。嗨,Neo,我已经按照您的建议发布了JSON。如何在ASP.Net服务器中查询参数。?但我有一个列表参数用于添加到请求中。如果我以字符串格式创建自己的JSON,我必须手动执行此操作ally?谢谢大家。嗨,Neo,我已经按照你的建议发布了JSON。我如何在ASP.Net服务器中查询参数。?