android http post 请求怎么写,我返回的结果不正确

 我来答
huanglenzhi
推荐于2016-07-27 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
采纳数:117533 获赞数:517215
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。

向TA提问 私信TA
展开全部

  HTTP错误405 – 禁止访问资源. 可能是server端有访问设置.你可以问下做server的人. 或者换一个server做下测试,看到底是server的问题,还是你代码的问题.

package jusi.singporecameratest;
 
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
 
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
 
public class Test extends Activity {
private TextView tv;
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 
String url = "http://appschallenge.juzz4.com/api/login";
HttpPost request = new HttpPost(url);
request.setHeader("Content-Type","application/x-www-form-urlencoded");
// 先封装一个 JSON 对象
JSONObject param = new JSONObject();
try{
param.put("username", "demo");
param.put("password", "demo");
param.put("mechanism", "plain");
// 绑定到请求 Entry
StringEntity se = new StringEntity(param.toString()); 
request.setEntity(se);
 
HttpResponse httpResponse = new DefaultHttpClient().execute(request);
// 得到应答的字符串,这也是一个 JSON 格式保存的数据
String retSrc = EntityUtils.toString(httpResponse.getEntity());
// 生成 JSON 对象
JSONObject result = new JSONObject(retSrc);
String token = (String) result.get("reason");
TextView tv = new TextView(this);
tv.setText(token);
setContentView(tv);}
catch(Exception e){ 
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式