尝试了这个但没有效果
public static void getQuestionsListApi2(final String requestId, final String timestamp,
final ImageProcessingCallback.downloadQuestionsCallbacks callback,
final Context context) {
try {
String url = NetUrls.downloadQuestions;
final JSONObject jsonBody = new JSONObject();
jsonBody.put("requestId", requestId);
jsonBody.put("timestamp", timestamp);
final String mRequestBody = jsonBody.toString();
Log.i("params", String.valueOf(jsonBody));
Log.i("URL", url);
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, **jsonBody**, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
Log.v("TAG", "Success " + jsonObject);
callback.downloadQuestionsCallbacksSuccess(jsonObject.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Log.v("TAG", "ERROR " + volleyError.toString());
}
});
request.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(request);
} catch (JSONException e) {
e.printStackTrace();
}
}
request.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 0,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
RequestQueue queue = Volley.newRequestQueue(context);
queue.add(request);
这是我在使用GET方法发送JSONRequest时使用的代码我收到来自服务器和服务器的400错误响应,但不包括网址格式中的数据。我发送 jsonBody 对象作为参数。任何解决方案。
答案 0 :(得分:0)
您可以使用改装来发送身体请求。 http://square.github.io/retrofit/
使用库很容易,例如:
@Get("[url node]")
Single<Response<ResponseBody>> doSmt(@Header("Authorization") String token, @Body ListRequest name);
另外,请看一下有关获取body的方法 HTTP GET with request body
答案 1 :(得分:0)
试试这段代码..
将以下依赖项添加到应用程序级别的gradle文件中。
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
然后在下面制作所有单独的类
First Retrofit对象创建类如下..
public class ApiClient {
private final static String BASE_URL = "https://dog.ceo/api/breed/";
public static ApiClient apiClient;
private Retrofit retrofit = null;
private Retrofit retrofit2 = null;
public static ApiClient getInstance() {
if (apiClient == null) {
apiClient = new ApiClient();
}
return apiClient;
}
//private static Retrofit storeRetrofit = null;
public Retrofit getClient() {
return getClient(null);
}
private Retrofit getClient(final Context context) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor() {
@Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
return chain.proceed(request);
}
});
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
}
然后像下面那样制作api界面..
public interface ApiInterface {
@POST("login/")
Call<LoginResponseModel> loginCheck(@Body UserData data);
}
使用pojo调用服务器响应和用户输入..
public class LoginResponseModel {
@SerializedName("message") // here define your json key
private String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
用户输入类
public class UserData {
private String email,password;
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
private void getLogin(){
ApiInterface apiInterface=ApiClient.getInstance().getClient().create(ApiInterface.class);
UserData data=new UserData();
data.setEmail("abc@gmail.com");
data.setPassword("123456");
Call<LoginResponseModel> loginResponseModelCall=apiInterface.loginCheck(data);
loginResponseModelCall.enqueue(new Callback<LoginResponseModel>() {
@Override
public void onResponse(Call<LoginResponseModel> call, retrofit2.Response<LoginResponseModel> response) {
if (response.isSuccessful() && response !=null && response.body() !=null){
LoginResponseModel loginResponseModel=response.body();
}
}
@Override
public void onFailure(Call<LoginResponseModel> call, Throwable t) {
}
});
}
当不需要用户间期时,使用GET方法。
你制作pojo类然后使用下面的链接它生成pojo类粘贴你的json数据.. http://www.jsonschema2pojo.org/
答案 2 :(得分:0)
如果要在GET请求的正文中传递Json数据,则必须使用查询注释
Call<YourNodelClass> getSomeDetails(@Query("threaded") String threaded, @Query("limit") int limit);
这将作为Json对象{"threaded": "val", "limit": 3}
通过。
我已经尝试过,而这只是工作代码。