android编写http请求,android java HttpPost请求怎么写

问题补充:

我发现用JSON了,换了这个方法还是同样的问题

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){

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值