2016-12-05 74 views
1

我有這樣的凌空GET請求類:傳遞字符串從片段到非活動類

public class VolleyGetHeaders { 
private static final String TAG = "VolleyGet"; 
private static VolleyGetHeaders instance = null; 

//for Volley API 
public RequestQueue requestQueue; 

private VolleyGetHeaders(Context context) 
{ 
    requestQueue = Volley.newRequestQueue(context.getApplicationContext()); 

    return; 
} 

public static synchronized VolleyGetHeaders getInstance(Context context) 
{ 
    if (null == instance) 
     instance = new VolleyGetHeaders(context); 
    return instance; 
} 

public static synchronized VolleyGetHeaders getInstance() 
{ 
    if (null == instance) 
    { 
     throw new IllegalStateException(VolleyGetHeaders.class.getSimpleName() + 
       " is not initialized, call getInstance(...) first"); 
    } 
    return instance; 

} 

public void VolleyGETHeaders(String domain, String api, final CustomListener<String> listener){ 

    StringRequest sr = new StringRequest(Request.Method.GET, domain +api, 
      new Response.Listener<String>() { 

       @Override 
       public void onResponse(String response) { 
        Log.e("HttpClient", "success! response: " + response); 
        if(null != response) 
         listener.getResult(response); 

        return; 

       } 
      }, 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        if (null != error.networkResponse) 
        { 
         Log.d(TAG + ": ", "Error Response code: " + error.networkResponse.statusCode); 
         //listener.getResult(false); 
        } 
       } 
      }) 
    { 

     @Override 
     public Map<String,String> getHeaders() throws AuthFailureError { 
      HashMap<String, String> headers= new HashMap<>(); 
      headers.put("Authorization",finalToken); 
      return headers; 
     } 
    }; 
    requestQueue.add(sr); 

} 
} 

而在我的片段我想我的字符串finalToken送入類設置爲標題:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    SharedPreferences sp=this.getActivity().getSharedPreferences("domainname", Context.MODE_PRIVATE); 
    String domain = sp.getString("domainname", ""); 
    SharedPreferences sp1=this.getActivity().getSharedPreferences("FINALTOKEN", Context.MODE_PRIVATE); 
    String finalToken = sp1.getString("FINALTOKEN",""); 
    VolleyGetHeaders.getInstance(getActivity()); 
    VolleyGetHeaders.getInstance().VolleyGETHeaders(domain, api, new CustomListener<String>(){ 
     @Override 
     public void getResult(String result){ 
      if (!result.isEmpty()) 
      { 
       Log.e("customer info", result); 
       System.out.println("customer info----------->" + result); 
      } 
     } 

    }); 

    return inflater.inflate(R.layout.my_account, container, false); 

我的問題是如何將finalToken傳遞給我的非活動類,因爲您可以看到我已經創建了最終的共享首選項,但我不知道如何在非活動類中檢索它。

回答

0

1)聲明一個變量(finalToken)非活性類:VolleyGetHeaders

public class VolleyGetHeaders { 
private static final String TAG = "VolleyGet"; 
private static VolleyGetHeaders instance = null; 
public RequestQueue requestQueue; 
String finalToken; 

private VolleyGetHeaders(Context context,String finalToken) 
    { 
    this.context=context; 
    this.finalToken=finalToken; 
    requestQueue = Volley.newRequestQueue(context.getApplicationContext()); 

    return; 
    } 

2)在您的片段發送給最終令牌太您調用的getInstance

用於例如

VolleyGetHeaders.getInstance(getActivity(),finalToken); 
+0

那解決了我的問題,謝謝! – JerryKo

+0

歡迎我的兄弟。 –

相關問題