/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package my.home.lehome.service.net;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
/**
* Created by legendmohe on 15/9/19.
*/
public class CommandRequest extends Request<String> {
private Response.Listener<String> mListener;
private String mCmd;
public CommandRequest(int method,
String url,
String cmd,
Response.Listener<String> listener,
Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.mListener = listener;
this.mCmd = cmd;
}
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("cmd", this.mCmd);
return params;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
String rep_str = new String(
response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(
rep_str,
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
public static String getJsonStringResponse(int code, String desc) {
JSONObject repObject = new JSONObject();
try {
repObject.put("code", code);
repObject.put("desc", desc);
} catch (JSONException je) {
}
return repObject.toString();
}
}