在我的應用程序中,我需要使用HttpPut方法向服務器發佈一些參數。所以爲此,我使用基本身份驗證技術進行了檢查,並以這種方式獲得相同的結果。但是當我試圖用Json Volley實現時,我無法得到結果。每次它拋出服務器錯誤。Android排除請求不與名稱值對一起工作
這裏是
@Override
protected Void doInBackground(String... params)
{
// TODO Auto-generated method stub
try
{
HttpPut request = new HttpPut(params[0]);
Log.d("debug", "Posting URL" + params[0]);
String creds = String.format("%s:%s",
"user123",
"abcd");
String auth = "Basic " + Base64.encodeToString(creds.getBytes(),
Base64.NO_WRAP);
request.setHeader("Authorization",
auth);
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("id", "409"));
urlParameters.add(new BasicNameValuePair("parent_id", "0"));
urlParameters.add(new BasicNameValuePair("content", "I am android developer"));
urlParameters.add(new BasicNameValuePair("email", "[email protected]"));
urlParameters.add(new BasicNameValuePair("username", "King Of Masses"));
request.setEntity(new UrlEncodedFormEntity(urlParameters));
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(request);
HttpEntity Entity = response.getEntity();
String jsondata = EntityUtils.toString(Entity);
Log.d("debug",
"Response Code:: " + response.getStatusLine().getStatusCode());
Log.d("debug",
"Json Data in Asynctask:: " + jsondata);
JSONObject ljsJsonObject=new JSONObject(jsondata);
Log.d("debug",
"Json Object Data:: " + ljsJsonObject.toString());
}
catch(ClientProtocolException e)
{
Log.d("debug",
"Exception" + e.toString());
e.printStackTrace();
}
catch(IOException e)
{
Log.d("debug",
"Exception" + e.toString());
e.printStackTrace();
}
catch(Exception e)
{
e.printStackTrace();
}
return null;
}
所以在這上面的方式,我得到的結果我的預期..但我無法實現與VolleyJson同我的代碼通過使用的AsyncTask基本身份驗證:: ..任何一個可以指導我如何做到這一點。我嘗試了太多的方法一樣(getBody(),getParams()方法),但遺憾的是沒有工作對我來說..
任何幫助將是非常感激。謝謝
謝謝!讓我試試@Zain :) –