Java Android JSON api存在PUT操作问题
我有一个在asp.NETMVC4中制作的web api,它公开了数据库的CRUD操作,我正在制作一个android应用程序来尝试和操作这些数据 我面临的问题是json解析器中的更新操作,该操作由http PUT请求(/api/products/id)公开 这是我使用的解析器,我从一个示例中获得,我修改了POST部分,因此它确实放了:Java Android JSON api存在PUT操作问题,java,android,json,asp.net-mvc-4,Java,Android,Json,Asp.net Mvc 4,我有一个在asp.NETMVC4中制作的web api,它公开了数据库的CRUD操作,我正在制作一个android应用程序来尝试和操作这些数据 我面临的问题是json解析器中的更新操作,该操作由http PUT请求(/api/products/id)公开 这是我使用的解析器,我从一个示例中获得,我修改了POST部分,因此它确实放了: public class JSONParser { static InputStream is = null; static JSONObject jObj =
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static JSONArray jArr = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if (method == "PUT") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPut httpPut = new HttpPut(url);
httpPut.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPut);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
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 (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
我做错了什么?三件事(其中两件更重要)
首先(重要),在处理响应正文之前,您需要确定您的请求是否成功。鉴于代码的结构,我建议在检索方法的开头声明一个状态代码变量,并在每次执行后设置它:
HttpResponse httpResponse = httpClient.execute(httpPut);
status = httpResponse.getStatusLine().getStatusCode();
第二个(重要),在调用响应实体上的任何方法之前,需要检查响应实体是否为非空。总的来说,使用EntityUtils
类来处理您的响应会更好
httpEntity = httpResponse.getEntity(); // Declare httpentity outside your try/catch
is = httpEntity.getContent(); // Remove this line entirely
然后,该实体将按如下方式进行处理:
try {
if(status == HttpStatus.SC_OK) {
json = httpentity != null ?
EntityUtils.toString(httpentity, "iso-8859-1") : null;
} else {
Log.e("Server responded with error status " + status);
}
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
最后(非常重要),您应该删除解析器类中的所有静态变量,并将它们改为方法的局部变量。似乎不是我面临的问题,操作确实成功了,是响应返回了问题
try {
if(status == HttpStatus.SC_OK) {
json = httpentity != null ?
EntityUtils.toString(httpentity, "iso-8859-1") : null;
} else {
Log.e("Server responded with error status " + status);
}
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}