我需要能够获取将由php代码生成的android中的http头部代码。在android中找出http响应代码
我对android非常陌生,无法掌握正确的做法。
目前我在我的signup.java文件中的代码能够发布一些数据到基于php的webservice,并且根据发送的数据,webservice回显一个json编码的响应。
PHP代码为响应在发生错误时是
$response["error"] = true;
$response["message"] = "Username Already taken";
echoRespnse(400,$response);
和上成功的代码是
$response["error"] = false;
$response["message"] = "Successfuly Registered";
echoRespnse(201,$response);
这将生成一个JSON ecoded响应消息。
我signup.java文件具有下面的代码由web服务所产生
public void post() throws UnsupportedEncodingException
{
// Get user defined values
uname = username.getText().toString();
email = mail.getText().toString();
password = pass.getText().toString();
confirmpass = cpass.getText().toString();
phone = phn.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpResponse httpResponse = null;
HttpPost httppost = new HttpPost("http://www.rgbpallete.in/led/api/signup");
if (password.equals(confirmpass)) {
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("uname", uname));
nameValuePairs.add(new BasicNameValuePair("pass", password));
nameValuePairs.add(new BasicNameValuePair("email", email));
nameValuePairs.add(new BasicNameValuePair("phone", phone));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpResponse = httpclient.execute(httppost); //http header response??
//Code to check if user was successfully created
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
Toast.makeText(getBaseContext(), "Password mismatch", Toast.LENGTH_SHORT).show();
//Reset password fields
pass.setText("");
cpass.setText("");
}
}
我想知道,如果httpResponse
将持有的HTTP标头响应代码或我需要解析JSON响应消息理解什么是由webserivce返回。
可能重复的[Android HttpResponse响应代码](http://stackoverflow.com/questions/8148765/android-httpresponse-response-code) – njzk2
不是真的,我在这里有2个问题,如果使用http标题是正确的方法和2如果'httpResponse = httpclient.execute(httppost)'将持有http头部代码。不想知道如何获取'http header' –
不清楚你在问什么。你试图检索什么标题,以及为了什么?你将得到的实际格式取决于你的php中'echoRespnse'的实现。通过'http header code'我假设你的意思是状态码? – njzk2