Java settHeader(“内容类型”、“内容类型”)-混淆
我有一个函数,我想用它将两个变量发布到php端,在这两个变量匹配并且服务器处理结果之后,我想用JSON返回结果。现在,我的set header属性如下所示:Java settHeader(“内容类型”、“内容类型”)-混淆,java,php,android,json,Java,Php,Android,Json,我有一个函数,我想用它将两个变量发布到php端,在这两个变量匹配并且服务器处理结果之后,我想用JSON返回结果。现在,我的set header属性如下所示: httppost.setHeader("Content-type", "application/json"); 但是,在阅读时,我发现内容类型应该是application/x-www-form-urlencoded,要接受JSON,应该是accept:application/JSON,我想更清楚地了解这一点,我如何修改代码以实现我想要的结
httppost.setHeader("Content-type", "application/json");
但是,在阅读时,我发现内容类型应该是application/x-www-form-urlencoded
,要接受JSON,应该是accept:application/JSON
,我想更清楚地了解这一点,我如何修改代码以实现我想要的结果?到目前为止,我使用的是本地主机,我的POST变量似乎没有在php端提供。以下是我的完整功能:
public void parse(String last, String pwd){
String lastIndex = last;
DefaultHttpClient http = new DefaultHttpClient(new BasicHttpParams());
System.out.println("URL is: "+CONNECT_URL);
HttpPost httppost = new HttpPost(CONNECT_URL);
httppost.setHeader("Content-type", "application/json");
try{
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("key", password));
nameValuePairs.add(new BasicNameValuePair("last_index", lastIndex));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
System.out.println("Post variables(Key): "+password+"");
System.out.println("Post variables(last index): "+lastIndex);
HttpResponse resp = http.execute(httppost);
HttpEntity entity = resp.getEntity();
ins = entity.getContent();
BufferedReader bufread = new BufferedReader(new InputStreamReader(ins, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = bufread.readLine()) != null){
sb.append(line +"\n");
}
result = sb.toString();
System.out.println("Result: "+result);
// readAndParseJSON(result);
}catch (Exception e){
System.out.println("Error: "+e);
}finally{
try{
if(ins != null){
ins.close();
}
}catch(Exception smash){
System.out.println("Squish: "+smash);
}
}
// return result;
}
public void解析(最后一个字符串,字符串pwd){
字符串lastIndex=last;
DefaultHttpClient http=新的DefaultHttpClient(新的BasicHttpParams());
System.out.println(“URL为:“+CONNECT\uURL”);
HttpPost HttpPost=新的HttpPost(连接URL);
setHeader(“内容类型”、“应用程序/json”);
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“密钥”,密码));
添加(新的BasicNameValuePair(“last_index”,lastIndex));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
System.out.println(“Post变量(键):“+password+”);
System.out.println(“Post变量(最后一个索引):”+最后一个索引);
HttpResponse resp=http.execute(httppost);
HttpEntity=resp.getEntity();
ins=entity.getContent();
BufferedReader bufread=新的BufferedReader(新的InputStreamReader(ins,“UTF-8”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=bufread.readLine())!=null){
sb.追加(第+行“\n”);
}
结果=sb.toString();
System.out.println(“结果:+Result”);
//readAndParseJSON(结果);
}捕获(例外e){
System.out.println(“错误:+e”);
}最后{
试一试{
如果(ins!=null){
ins.close();
}
}捕获(异常粉碎){
System.out.println(“挤压:+smash”);
}
}
//返回结果;
}
看起来您的代码实际上在做文章描述的事情,除了
// httppost.setHeader("Content-type", "application/json");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/json");
您正在此处添加x-www-form-urlencoded内容
看起来您的代码实际上在做文章描述的事情,除了
// httppost.setHeader("Content-type", "application/json");
httppost.setHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setHeader("Accept", "application/json");
您正在此处添加x-www-form-urlencoded内容
你有个问题。尝试“内容类型”而不是“内容类型”(或使用const
HTTP.Content\u Type
)。您有caps问题。尝试“内容类型”而不是“内容类型”(或者使用constHTTP.Content\u Type
)。那么建议是什么?我对我的代码的有效性有点怀疑,因为POST变量似乎在传输过程中丢失了。@user2822178否。服务器删除它们是因为它没有查找urlencoded表单数据。考虑使用像FIDDLR2这样的日志代理服务器,然后你可以看到你在有线级别上的发送和接收。非常感谢,我的下一站将是日志代理服务器!那么建议是什么呢?我对我的代码的有效性有点怀疑,因为POST变量似乎在传输过程中丢失了。@user2822178否。服务器删除它们是因为它没有查找urlencoded表单数据。考虑使用像FIDDLR2这样的日志代理服务器,然后你可以看到你在有线级别上的发送和接收。非常感谢,我的下一站将是日志代理服务器!真的,伙计,我现在可以发布变量了!真的,伙计,我现在可以发布变量了!