我看到关于Volley的Google IO 2013会议,我正在考虑转向凌空.Volley是否支持添加POST/GET参数来请求?如果是,我该怎么办?
我正在尝试在Volley JsonObjectRequest中发送POST参数.最初,它通过遵循官方代码所说的传递包含JsonObjectRequest的构造函数中的参数的JSONObject来为我工作.然后它突然停止工作,我没有对以前工作的代码进行任何更改.服务器不再识别正在发送任何POST参数.这是我的代码:
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://myserveraddress";
// POST parameters
Map<String, String> params = new HashMap<String, String>();
params.put("tag", "test");
JSONObject jsonObj = new JSONObject(params);
// Request a json response from the provided URL
JsonObjectRequest jsonObjRequest = new JsonObjectRequest
(Request.Method.POST, url, jsonObj, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_SHORT).show();
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
});
// Add the request to …Run Code Online (Sandbox Code Playgroud) 我已经使用Apache 301重定向将SEO URL实现到网站根目录中的"redirect.cfm",该网站处理所有URL构建和内容交付.
在301重定向期间,发布数据会丢失.
到目前为止还无法找到解决方案,尝试从重写中排除post方法 - 最坏的情况我们可以使用旧类型的URL来进行post方法.
有什么可以做的吗?
谢谢
目前我正在片段中的停止方法上使用mRequestQueue.cancelAll(getActivity()),但显然当我将手机从横向移动到肖像时,它仍然返回请求中的数据但导致崩溃,因为持有者为数据剂量已经存在.任何正确的代码示例代码?
我正在对旧项目进行更新,目前我对Android的知识不多.在项目中,我们有关于产品的评论部分.
为了在发送之前发表评论,我们返回0(一些错误)和1(成功).
以下是我们使用的代码.
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Method.POST,
act.getString(R.string.CommentForUserURL),
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(
JSONObject response) {
Log.d("response done", "done===" + response);
mloading.setVisibility(View.GONE);
if (response != null) {
Comment obj = new Comment();
JSONObject jsonObject = response;
try {
obj.setComment(jsonObject
.getString("Comment"));
Run Code Online (Sandbox Code Playgroud)
现在我们将返回对象从0/1更改为用户对象.
这需要将JsonObjectRequest更新为GJSON请求吗?或者对象也将使用JsonObjectRequest进行解析?
我问,因为当我执行上面的时候,我得到如下错误.
01-25 12:30:21.754: E/Volley(16487): [10114] BasicNetwork.performRequest:
Unexpected response code 500 for
http://new.souqalharim.com/add/CommentForMerchant
Run Code Online (Sandbox Code Playgroud)
知道我为什么会收到这个错误吗?
注意:此URL适用于iPhone应用程序.
这是post方法,所以没有完整的url.还有更多参数要添加,例如:comment = MyComment&userId = 123&productId = 234.因为它是post我不是在实际url中添加参数.
我有其他方法
@Override
protected Map<String, String> getParams()
throws AuthFailureError {
Map<String, …Run Code Online (Sandbox Code Playgroud) 我正在尝试连接API url ="api adress",它接受两个头类型application/json来响应json和application/xml以在xml中重新定义.我需要使用json参数命中JSON,并且响应也将采用json格式.使用android volley Post请求和JsonObjectRequest设置头使用getHeaders它连接到服务器但getParams设置参数不起作用.
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
Constants.BASE_URL + Constants.LOGIN, null, response,
response_error) {
/**
* Passing some request headers
* */
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
return headers;
}
@Override
protected Map<String, String> getPostParams()
throws AuthFailureError {
// TODO Auto-generated method stub
Map<String, String> params = new HashMap<String, String>();
params.put("key", "value");
return params;
}
};
// implementation of listeners …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Volley库将JSONObject POST请求发送到服务器,该服务器接受2个参数,一个对象(地址)和一个不同对象列表(租户).
当我尝试发出请求时,第一个参数(地址)在发送之前由Volley格式化,并且服务器不接受该请求.
我的请求看起来像这样:
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, SERVER_URL,
postPropertyJSONObject, responseListener, errorListener)
Run Code Online (Sandbox Code Playgroud)
我的postPropertyJSONObject是这样创建的:
JSONObject obj = new JSONObject();
obj.put("Address", convertAddressToJson(property.getAddress()).toString());
obj.put("Tenants", prop.getTenantList());
Run Code Online (Sandbox Code Playgroud)
convertAddressToJson()方法如下所示:
JSONObject jsonObject= new JSONObject();
jsonObject.put("Building", address.getBuilding());
jsonObject.put("Street", address.getStreet());
jsonObject.put("Town", address.getTown());
jsonObject.put("ZipCode", address.getZipCode());
jsonObject.put("County", address.getCounty());
jsonObject.put("Country", address.getCountry());
Run Code Online (Sandbox Code Playgroud)
我试过传递Address对象,但这根本没有序列化,所以它也不起作用.我还尝试在请求中使用的JSONObject的"Address"字段中传递Address.toString(),但这也不起作用.
我的Property对象的getAddress()方法返回一个Address对象,如下所示:
public class Address {
private String Street;
private String Building;
private String Town;
private String ZipCode;
private String County;
private String Country;
// getters and setters
}
Run Code Online (Sandbox Code Playgroud)
当我在传递地址之前记录地址时,请求看起来像这样:
{"Town":"MyTown","Street":"MyStreet","County":"MyCounty","Country":"MyCountry",
"ZipCode":"MyZipCode","Building":"MyBuilding"}
Run Code Online (Sandbox Code Playgroud)
但是当服务器记录它收到的内容时,它看起来像这样:
{\"Town\":\"MyTown\",\"Street\":\"MyStreet\",\"County\":\"MyCounty\",
\"Country\":\"MyCountry\",\"ZipCode\":\"MyZipCode\",\"Building\":\"MyBuilding\"}"
Run Code Online (Sandbox Code Playgroud)
Volley应用的这种格式似乎正在改变我通过我的请求传递的价值所以任何人都可以告诉我是否有更好的方法来处理这个应该相对简单的任务?我使用String和Integer值向同一服务器发出了请求,但在尝试将自定义类作为参数传递时遇到了这个问题.
编辑
使用wbelarmino的提示,我切换到使用hashmap存储我的自定义对象并从中创建了一个JSONObject:
HashMap<String, Address> …Run Code Online (Sandbox Code Playgroud) 每次我尝试使用Volley的POST方法时,都会出现严重错误.我在getCause中得到null值,在getNetworkResponse.toString()中得到一些默认值.
如果我使用GET方法,这工作正常(我从我的网址得到回复).
有人可以帮忙吗?
Map<String, String> jsonParams = new HashMap<String, String>();
jsonParams.put("teststr", "abd");
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue();
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.POST,
url,
new JSONObject(jsonParams),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(), "Success"+response.toString(), Toast.LENGTH_LONG).show();
}catch(Exception e){
Toast.makeText(getApplicationContext(), "JSON ERROR", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("abd", "Error: " + error
+ ">>" + error.networkResponse.statusCode
+ ">>" + error.networkResponse.data
+ ">>" + error.getCause()
+ ">>" + error.getMessage());
}
}) …Run Code Online (Sandbox Code Playgroud) 请问,Volley会自动将我的GET参数添加到URL吗?对我来说,它不是这样工作,也是在查看源代码时,我无法找到任何getParams方法的调用.那么我应该自己构建URL吗?这根本不是问题,我只是认为当有像getParams这样的方法时,它可以为我做到这一点:)
更新:以下是我的代码..
public class BundleRequest extends com.android.volley.Request<Bundle>{
private String token;
private OnAuthTokenValidatorResponseListener mListener;
private final Map<String, String> mParams = new HashMap<String, String>();;
public BundleRequest(int method, String url, Response.ErrorListener listener) {
super(method, url, listener);
}
public BundleRequest(int method, String url,OnAuthTokenValidatorResponseListener providedListener, Response.ErrorListener listener, String token) {
super(method, url, listener);
this.token = token;
mListener = providedListener;
mParams.put(AuthenticatorConfig.TOKEN_VALIDATION_PARAMNAME, token);
}
@Override
public Map<String, String> getParams() throws AuthFailureError {
return mParams;
}
@Override
protected Response<Bundle> parseNetworkResponse(NetworkResponse httpResponse) {
switch (httpResponse.statusCode) {
case AuthTokenValidator.TOKEN_VALID_RESPONSE_CODE:
//token …Run Code Online (Sandbox Code Playgroud) 运行应用程序时出现以下错误:BasicNetwork.performRequest:意外的响应代码401
我需要传递电子邮件,密码和令牌来访问URL,但它无法正常工作
我上周开始学习android,我不太了解
package quest.testvolley;
import com.android.volley.AuthFailureError;
import com.android.volley.VolleyLog;
import com.kpbird.volleytest.R;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONObject;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends Activity {
private String TAG = this.getClass().getSimpleName();
private ListView lstView;
private RequestQueue mRequestQueue;
private ArrayList<NewsModel> arrNews ;
private LayoutInflater …Run Code Online (Sandbox Code Playgroud) android ×9
json ×3
post ×2
apache ×1
content-type ×1
get ×1
http-post ×1
httprequest ×1
jsonobject ×1
networking ×1
parameters ×1
rewrite ×1