2014-12-18 95 views
0

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

+0

可能重複的[Android HttpResponse響應代碼](http://stackoverflow.com/questions/8148765/android-httpresponse-response-code) – njzk2

+0

不是真的,我在這裏有2個問題,如果使用http標題是正確的方法和2如果'httpResponse = httpclient.execute(httppost)'將持有http頭部代碼。不想知道如何獲取'http header' –

+0

不清楚你在問什麼。你試圖檢索什麼標題,以及爲了什麼?你將得到的實際格式取決於你的php中'echoRespnse'的實現。通過'http header code'我假設你的意思是狀態碼? – njzk2

回答

2

響應代碼可以檢索這樣:

final int statusCode = httpResponse.getStatusLine().getStatusCode(); 
switch (statusCode) 
{ 
    case 201: 
     // Successfuly Registered 
     break; 
    case 400: 
     // Username Already taken 
     break; 
    default: 
     break; 
} 

要列出的標頭值,則可以使用:

for (final Header h : httpResponse.getAllHeaders()) 
{ 
    Log.d("Responser Header", h.getName() + ": " + h.getValue()); // or h.getElements() 
} 

,或者直接挑所需的標題:

final String[] contentLength = httpResponse.getHeaders("Content-Length"); 

並解析它。

+0

Will just使用代碼'statusCode'的第一部分做還是我需要列出標題值? –

+1

爲了識別狀態碼,第一部分就足夠了。如果您需要任何標題值(如特定消息),您可能需要使用2.1或2.2部分的答案。 – rekaszeru

相關問題