这是我的doInBackground函数:
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
if(type.equals("POST") || type.equals("PUT")){
params.add(new BasicNameValuePair("id", pid));
params.add(new BasicNameValuePair("name", name));
}else if(type.equals("DELETE")){
Log.d("DELETE:", pid);
params.add(new BasicNameValuePair("id", pid));
}
JSONObject json = jsonParser.makeHttpRequest(url, type, params);
Log.d("Create Response", json.toString());
if(type.equals("GET")){
try {
jsonRows = json.getJSONArray("rows");
for(int i = 0; i<jsonRows.length();i++){
JSONObject p = (JSONObject)jsonRows.get(i);
aList.add(p.getString("ID") + " - " +p.getString("Name"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}else{
try{
String result = json.getString("results");
return result;
}catch(JSONException e){
e.printStackTrace();
}
}
return null;
}
我的httpDelete代码:
else if(method == "DELETE"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String param = null;
for (NameValuePair nvp : params){
param = nvp.getValue();
url += "/" + param;
Log.d("url = ", url);
}
HttpDelete httpDelete = new HttpDelete(url);
HttpResponse httpResponse = httpClient.execute(httpDelete);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
和node.js服务器:
app.delete('/api/db/:id', function(req, res){
var id = req.params.id;
res.setHeader('content-type', 'application/json');
dbPool.getConnection(function(objErr, objConn){
if(objErr){
sendError(res, 503, 'error', 'connection', objErr); //503 - Service Unavailable
}else{
objConn.query("DELETE FROM person WHERE ID = ? ",[id], function(Err, Rows, Fields){
if(Err){
sendError(res, 500, 'error', 'query', Err);
}else{
res.send({
results : 'success'
});
objConn.release();
gcmSend();
}//else
});
}//else
});
});
错误:
09-24 06:35:12.386: E/AndroidRuntime(1908): FATAL EXCEPTION: AsyncTask #4
09-24 06:35:12.386: E/AndroidRuntime(1908): java.lang.RuntimeException: An error occured while executing doInBackground()
09-24 06:35:12.386: E/AndroidRuntime(1908): at android.os.AsyncTask$3.done(AsyncTask.java:299)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
09-24 06:35:12.386: E/AndroidRuntime(1908): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.lang.Thread.run(Thread.java:841)
09-24 06:35:12.386: E/AndroidRuntime(1908): Caused by: java.lang.IllegalArgumentException: Illegal character in path at index 36: -----192.168.1.11:3030/api/db/9
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.net.URI.create(URI.java:727)
09-24 06:35:12.386: E/AndroidRuntime(1908): at org.apache.http.client.methods.HttpDelete.<init>(HttpDelete.java:68)
09-24 06:35:12.386: E/AndroidRuntime(1908): at com.argenit.monitor.JSONParser.makeHttpRequest(JSONParser.java:86)
09-24 06:35:12.386: E/AndroidRuntime(1908): at com.argenit.monitor.MainPage$HttpAsyncTask.doInBackground(MainPage.java:137)
09-24 06:35:12.386: E/AndroidRuntime(1908): at com.argenit.monitor.MainPage$HttpAsyncTask.doInBackground(MainPage.java:1)
09-24 06:35:12.386: E/AndroidRuntime(1908): at android.os.AsyncTask$2.call(AsyncTask.java:287)
09-24 06:35:12.386: E/AndroidRuntime(1908): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
09-24 06:35:12.386: E/AndroidRuntime(1908): ... 4 more
有人帮助我吗?
错误前的最后一次logcat:
Log.d(“url =”,url); - &GT; 09-24 06:40:45.066:D / url =(1978): ---- / 192.168.1.11:3030/api/db/8