之前我已经实现了凌空请求并且它工作正常,我做了一些更改,我按照我想要的方式停止工作(我会解释代码后的更改)。
所以基本上我想请求凌空传递4个参数(方法,网址,令牌和数据)
所以我这样做:
IResult mResultCallback2 = null;
public void sendFotoServer(){
String url = connectionTxt + "/fotos/" + specieId + "/plants";
HashMap<String,String> params = new HashMap<String, String>();
byte[] capture = encodeImage(b);
String encodedImage = Base64.encodeToString(capture,1);
Log.d("encodedImage",encodedImage);
params.put("base64", encodedImage);
params.put("lat",String.valueOf(photo.getLat()));
params.put("lon",String.valueOf(photo.getLon()));
params.put("alt",String.valueOf(photo.getAlt()));
params.put("date",photo.getTime());
JSONObject sendObj = new JSONObject(params);
PostPhotoVolleyCallback();
mVolleyService = new VolleyService(mResultCallback2, this);
mVolleyService.postDataVolley("POSTCALL", url, sendObj,token);
}
void PostPhotoVolleyCallback(){
mResultCallback2 = new IResult() {
@Override
public void notifySuccess(String requestType, JSONObject response) {
Log.d("HELLO","HELLOTHERE");
Log.d("resposta2",response.toString());
Intent intent = new Intent(SimiliarPhotos.this,PlantFeed.class);
startActivity(intent);
}
@Override
public void notifySuccess(String requestType, JSONArray response) {
Log.d("HELLO",response.toString());
}
@Override
public void notifyError(String requestType, VolleyError error) {
Log.d("errorV",error.getStackTrace().toString());
}
};
}
我已经定义了一个界面,所以我可以重复使用这样的排球调用:
package com.example.afcosta.inesctec.pt.android.services;
import android.content.Context;
import android.util.Log;
import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
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.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.afcosta.inesctec.pt.android.interfaces.IResult;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
/**
* Created by FilipeCosta on 19/05/2017.
*/
public class VolleyService {
IResult mResultCallback = null;
Context mContext;
public VolleyService(IResult resultCallback, Context context){
mResultCallback = resultCallback;
mContext = context;
}
public void postDataVolleyAuth(final String requestType,String url,JSONObject sendObj){
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if(mResultCallback != null) {
mResultCallback.notifySuccess(requestType, response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (mResultCallback != null) {
mResultCallback.notifyError(requestType, error);
}
}
});
jsonObj.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObj);
}catch(Exception e){
}
}
public void postDataVolley(final String requestType,String url,JSONObject sendObj,final String token){
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if(mResultCallback != null) {
mResultCallback.notifySuccess(requestType, response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(mResultCallback != null) {
mResultCallback.notifyError(requestType,error);
}
}
}) {
@Override
public Map<String, String> getHeaders() {
Map<String, String> headers = new HashMap<>();
Log.d("tokenXX", token);
headers.put("x-access-token", token);
return headers;
}
};
jsonObj.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObj);
}catch(Exception e){
}
}
public void getDataVolley(final String requestType, String url, final String token){
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonArrayRequest jsonArray = new JsonArrayRequest(Request.Method.GET, url,null, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
if(mResultCallback != null)
mResultCallback.notifySuccess(requestType, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<>();
Log.d("tokenXX",token);
headers.put("x-access-token", token);
return headers;
}
};
jsonArray.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonArray);
}catch(Exception e){
}
}
public void getDataObjectVolley(final String requestType, String url, final String token){
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if(mResultCallback != null)
mResultCallback.notifySuccess(requestType, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
public Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<>();
Log.d("tokenXX",token);
headers.put("x-access-token", token);
return headers;
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObjectRequest);
}catch(Exception e){
}
}
public void putDataVolley(final String requestType, String url, final String token){
try {
RequestQueue queue = Volley.newRequestQueue(mContext);
JsonObjectRequest jsonObj = new JsonObjectRequest(Request.Method.PUT,url,null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if(mResultCallback != null) {
mResultCallback.notifySuccess(requestType, response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if(mResultCallback != null) {
mResultCallback.notifyError(requestType,error);
}
}
}){
@Override
public Map<String, String> getHeaders(){
Map<String, String> headers = new HashMap<>();
Log.d("tokenXX",token);
headers.put("x-access-token", token);
return headers;
}
};
jsonObj.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
queue.add(jsonObj);
}catch(Exception e){
}
}
}
问题是我得到了回复,但就像5分钟之后,就像这样:
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
POST /fotos/10/plants - - ms - -
我唯一改变的是我获取此活动数据的方式,开始时我的模型是可序列化的,我将其更改为parcelable,我将我的图像传递给文件并将其转换为byte []并在之后base64,我试图调试,我的所有数据都存在,这里有任何提示吗?
谢谢:)