Android : JSON POST instead of PUT

on Sunday, October 12, 2014


I need to send put request to server, but my app is sending post request instead of it, I've tried to find the problem but I can't find any error public class JSON { public static JSON createEmpty(){ return new JSON("{}"); }



public static JSON createFromNet(String url){
return JSON.createFromNet(url, "GET", "");
}

public static JSON createFromNet(String url, String method, JSON params){
return JSON.createFromNet(url, method, params.toString());
}

public static JSON createFromNet(String url, String method, String params){
String json = "{}";
try{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpUriRequest httpRequest = null;
if (method == "GET"){
httpRequest = JSON.getGetRequest(url);
}
if (method == "PUT"){
httpRequest = JSON.getPutRequest(url, params);
}
if (httpRequest == null){
throw new Exception();
}
httpRequest.setHeader("XXXXXXX", "XXXXXXX");

HttpResponse httpResponse = httpClient.execute(httpRequest);
HttpEntity httpEntity = httpResponse.getEntity();
InputStream is = httpEntity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}catch (UnsupportedEncodingException e) {
}catch (ClientProtocolException e) {
}catch (IOException e) {
}catch (Exception e) {
}

return new JSON(json);
}

protected static HttpUriRequest getPutRequest(String url, String params) throws UnsupportedEncodingException{
StringEntity input = new StringEntity(params);
input.setContentType("application/json");
input.setContentEncoding("UTF-8");

HttpPut request = new HttpPut(url);
request.setEntity(input);


return request;
}

protected static HttpUriRequest getGetRequest(String url){
return new HttpGet(url);
}

protected JSONObject json = null;

public JSON(String json_string){
try {
json = new JSONObject(json_string);
} catch (JSONException e) {
json = new JSONObject();
}
}

public JSON(JSONObject json){
this.json = json;
}

public int getInt(String key, int def){
try{ return json.getInt(key); }
catch (Exception e) { return def; }
}

public String getString(String key, String def){
try{ return json.getString(key); }
catch (Exception e) { return def; }
}

public double getDouble(String key, double def){
try{ return json.getDouble(key); }
catch (Exception e) { return def; }
}

public JSON getObject(String key){
try{ return new JSON(json.getJSONObject(key));
} catch (Exception e){ return new JSON("{}"); }
}

public ArrayList<JSON> getArray(String key){
try{
ArrayList<JSON> result = new ArrayList<JSON>();
JSONArray json_array = json.getJSONArray(key);
for (int i = 0; i < json_array.length(); i++){
try{ result.add(new JSON(json_array.getJSONObject(i)));}
catch (Exception e){}
}
return result;
}
catch (Exception e){
return new ArrayList<JSON>();
}
}

public String toString(){
try{ return json.toString(); }
catch (Exception e) { return "{}"; }
}

public void putString(String key, String value){
try{ json.put(key, value);}
catch (Exception e) {}
}

public void putInt(String key, Integer value){
try{ json.put(key, value);}
catch (Exception e) {}
}

public void putDouble(String key, Double value){
try{ json.put(key, value);}
catch (Exception e) {}
}

public void putObject(String key, JSON value){
try{
json.put(key, new JSONObject(value.toString()));
}
catch (Exception e) {}
}

public void putArray(String key, ArrayList<JSON> value){
try{
JSONArray json_array = new JSONArray();
for (int i = 0; i < value.size(); i++){
json_array.put(new JSONObject(value.get(i).toString()));
}
json.put(key, json_array);}
catch (Exception e) {}
}
}

0 comments:

Post a Comment