
android http post 请求怎么写,我返回的结果不正确
1个回答
推荐于2016-07-27 · 知道合伙人数码行家

知道合伙人数码行家
采纳数: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){
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询