2011-12-16 96 views
0

在我的代碼中,我將數據發佈到web服務,但是我從web服務獲得的響應是​​指定的數據爲空。這意味着我的請求正在被處理web服務,但它不能獲取相應的鍵值。Android:使用JSON將數據發佈到web服務

代碼:

Button b1=(Button)findViewById(R.id.button1); 
     b1.setOnClickListener(new OnClickListener() 
     { 

      public void onClick(View v) { 
       String gaurav ="new_member="+"[{\"email\":\"[email protected]\",\"username\":\"gaurav001\",\"pwd\":\"gaurav001\"}]"; 
       // TODO Auto-generated method stub 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("url"); 

try{ 
    String d="[email protected]"; 
        JSONObject json = new JSONObject(); 

        json.put("gaurav", gaurav); 
        // json.put("api_token",settings.getString("api_token", "")); 
        StringEntity se = new StringEntity(json.toString()); 
        httppost.setEntity(se); 
        httppost.setHeader("Accept", "application/json"); 
        httppost.setHeader("Content-type", "application/json"); 
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 


        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity responseEntity =response.getEntity(); 

        Log.e("USER", EntityUtils.toString(responseEntity).trim()); 
} catch (UnsupportedEncodingException e11) { 
        // TODO Auto-generated catch block 
        Log.e("USER", e11.getMessage()); 
       } catch (ClientProtocolException e11) { 
        // TODO Auto-generated catch block 
        Log.e("USER", e11.getMessage()); 
       } catch (IOException e11) { 
        // TODO Auto-generated catch block 
        Log.e("USER", e11.getMessage()); 
       } catch (JSONException e11) { 
        // TODO Auto-generated catch block 
        e11.printStackTrace(); 
       } 

      } 

     } 
       ); 

來自服務器的響應是:

12-16 13:59:47.542: E/USER(1257): [{"errorcode":"012"}] 

從服務器響應列表:

012 You must enter your e-mail address 
013 You must enter your username 
014 The username you chose is already taken 
015 Your e-mail address is invalid 
016 The e-mail address you entered is already taken 
017 You must enter a password 
025 Your username should have at least 4 characters 
024 Username can only contain letters A-Z and numbers 0-9 

我得到了所有被罰款會感覺,那要麼是我的弦高盧;文字是問題。她是我必須按照web服務規範通過的字符串:

new_member = [{「email」:「[email protected]」,「username」:「gaurav001」,「pwd」:「gaurav001」}]

我已經嘗試了很多次,但仍然收到了錯誤代碼:12。

回答

2

我在做什麼是一樣的東西,

JSONObject jsonObject = new JSONObject(); 
jsonObject.put("email", "[email protected]"); 
jsonObject.put("uname", "test"); 
jsonObject.put("upwd", "test123"); 

JSONArray jArrayParam = new JSONArray(); 
jArrayParam.put(jsonObject); 

List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(); 
nameValuePair.add(new BasicNameValuePair("bulkdata",jArrayParam.toString())); 

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost(url); 
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded"); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8)); 

// Execute HTTP Post Request 
HttpResponse response = httpclient.execute(httppost); 

使用bulkdata你可以在服務器端獲取這些值..

相關問題