Android HTTP Post请求错误400
小弟刚接触Android,想做个简单的Coursera的客户端,登录POST数据是,登录用户名密码正确,总是出现400请求错误,跪求大神们不吝赐教...登录代码如下sig...
小弟刚接触Android,想做个简单的Coursera的客户端,登录POST数据是,登录用户名密码正确,总是出现400请求错误,跪求大神们不吝赐教...登录代码如下
sign_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String urlPath = "https://accounts.coursera.org/api/v1/login";
Map<String, String> params = new HashMap<String,String>();
params.put("email",username_edit.toString());
params.put("password",password_edit.toString());
String encoding = HTTP.UTF_8;
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
UrlEncodedFormEntity entitydata = null;
try {
entitydata = new UrlEncodedFormEntity(paramPairs,
encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpPost post = new HttpPost(urlPath);
post.setEntity(entitydata);
DefaultHttpClient client = new DefaultHttpClient();
// 执行post请求
try {
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 200) {//不成功重新登录,显示错误码
int i = response.getStatusLine().getStatusCode();
CharSequence cs=String.valueOf(i);
login_user_input.setText(cs);
} else {
Intent intent = new Intent();
intent.setClass(Login.this,Interface.class);
startActivity(intent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}); 展开
sign_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String urlPath = "https://accounts.coursera.org/api/v1/login";
Map<String, String> params = new HashMap<String,String>();
params.put("email",username_edit.toString());
params.put("password",password_edit.toString());
String encoding = HTTP.UTF_8;
List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
paramPairs.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
UrlEncodedFormEntity entitydata = null;
try {
entitydata = new UrlEncodedFormEntity(paramPairs,
encoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpPost post = new HttpPost(urlPath);
post.setEntity(entitydata);
DefaultHttpClient client = new DefaultHttpClient();
// 执行post请求
try {
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() != 200) {//不成功重新登录,显示错误码
int i = response.getStatusLine().getStatusCode();
CharSequence cs=String.valueOf(i);
login_user_input.setText(cs);
} else {
Intent intent = new Intent();
intent.setClass(Login.this,Interface.class);
startActivity(intent);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}); 展开
1个回答
2014-04-10
展开全部
你确定你的post方法调用的url没问题吗?参数的名称和个数也没问题吗?
更多追问追答
追答
官方的文档有提到login这个接口吗?只有这货:https://accounts.coursera.org/oauth2/v1/auth
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询