我需要能夠獲取將由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