2016-04-27 47 views

回答

0

需要使用StringRequest作爲提到的djodjo。 也getBody方法需要被覆蓋 - 從這裏Android Volley POST string in body

@Override 
       public byte[] getBody() throws AuthFailureError { 
        String httpPostBody="your body as string"; 
        // usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it 
        try { 
         httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+ URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8"); 
        } catch (UnsupportedEncodingException exception) { 
         Log.e("ERROR", "exception", exception); 
         // return null and don't pass any POST string if you encounter encoding error 
         return null; 
        } 
        return httpPostBody.getBytes(); 
       } 
+0

你不需要那樣做。排球可以處理這些。請檢查我更新的答案。 – djodjo

1

例如:

final TextView mTextView = (TextView) findViewById(R.id.text); 
... 

// Instantiate the RequestQueue. 
RequestQueue queue = Volley.newRequestQueue(this); 
String url ="http://www.google.com"; 

// Request a string response from the provided URL. 
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, 
      new Response.Listener<String>() { 
    @Override 
    public void onResponse(String response) { 
     // Display the first 500 characters of the response string. 
     mTextView.setText("Response is: "+ response.substring(0,500)); 
    } 
}, new Response.ErrorListener() { 
    @Override 
    public void onErrorResponse(VolleyError error) { 
     mTextView.setText("That didn't work!"); 
    } 
}); 
// Add the request to the RequestQueue. 
queue.add(stringRequest); 

檢查the source and more info here

**更新:**如果您需要添加PARAMS你可以簡單地覆蓋getParams()

例子:

@Override 
protected Map<String, String> getParams() throws AuthFailureError { 
     Map<String, String> params = new HashMap<String, String>(); 
     params.put("param1", "val1"); 
     params.put("randomFieldFilledWithAwkwardCharacters","{{%stuffToBe Escaped/"); 
     return params; 
    } 

你不需要覆蓋getBody你的精靈不會編碼特殊的字符,因爲沃利正在爲你做這件事。

+0

字符串確實要求採取。你還需要重寫public byte [] getBody() – BoazGarty

+0

你不需要。你沒有指定你想要的東西。檢查更新。 – djodjo