2015-07-06 28 views
1

的Android編程:將數據發送到服務器中使用VOLLEY如何使用排球庫發送字符串?

是新來的凌空庫。

我有以下的EditText用戶輸入數據,並按下注冊按鈕。我想將數據發送到hostinger託管的遠程服務器。

EditText firstname = (EditText)findViewById(R.id.firstname); 
EditText lastname = (EditText)findViewById(R.id.lastname); 
EditText email  = (EditText)findViewById(R.id.email); 
EditText phone  = (EditText)findViewById(R.id.phone); 
EditText password = (EditText)findViewById(R.id.password); 
EditText repeatpass = (EditText)findViewById(R.id.rptpassword); 

Button regbtn = (Button)findViewById(R.id.regbtn); 
Button tologin = (Button)findViewById(R.id.loginbtn); 

如何將數據發佈到服務器?

請包括服務器端的代碼。

回答

1

一個簡單的字符串要求:

RequestQueue queue = MyVolley.getRequestQueue(); 
       StringRequest myReq = new StringRequest(Method.GET, 
                 "http://www.google.com/", 
                 createMyReqSuccessListener(), 
                 createMyReqErrorListener()); 

queue.add(myReq); 

} 

private Response.Listener<String> createMyReqSuccessListener() { 
     return new Response.Listener<String>() { 
      @Override 
      public void onResponse(String response) { 


       //TODO 

      } 
     }; 
    } 


    private Response.ErrorListener createMyReqErrorListener() { 
     return new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

        //TODO 
      } 
     }; 
    } 

而在MyVolley類:

public static RequestQueue getRequestQueue() { 
     if (mRequestQueue != null) { 
      return mRequestQueue; 
     } else { 
      throw new IllegalStateException("RequestQueue not initialized"); 
     } 
    } 

如果你想添加字符串params爲簡單:

String uri = "www.google.com" + params; 

RequestQueue queue = MyVolley.getRequestQueue(); 
        StringRequest myReq = new StringRequest(Method.GET, 
                  uri, 
                  createMyReqSuccessListener(), 
                  createMyReqErrorListener()); 

    queue.add(myReq); 

    } 
+0

非常感謝您 –

+0

@SimonWanjau不客氣!如果你高興,請接受答案,謝謝 –