我目前正在开发一个应用程序,并且在将 JSONObject 上传到我的 Iris CouchDb 时遇到问题。但我无法让它工作。
这是我现在使用的代码:
private class MyHttpPost extends AsyncTask<String, Void, Boolean> {
@Override
protected Boolean doInBackground(String... arg0)
{
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
HttpVersion.HTTP_1_1);
HttpClient httpClient = new DefaultHttpClient(params);
HttpPost httpPost = new HttpPost("https://dbname.iriscouch.com/dbname");
try {
// Add your data
JSONObject jsonDoc = new JSONObject();
try {
jsonDoc.put("name", "test");
jsonDoc.put("autor", "test author");
jsonDoc.put("rundgangnr", "1");
jsonDoc.put("latitude", 58.0);
jsonDoc.put("longitude", 7.88);
} catch (JSONException e) {
e.printStackTrace();
} // end try
String body = jsonDoc.toString();
StringEntity entity = new StringEntity(body, "utf-8");
httpPost.setEntity(entity);
// Execute HTTP Post Request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity httpEntity = response.getEntity();
InputStream result = httpEntity.getContent();
} catch (IOException e) {
// TODO Auto-generated catch block
}
return true;
}
}
在 onCreate 我这样做是为了执行函数:
new MyHttpPost().execute();
当我运行该应用程序时,没有错误,但没有上传任何内容。所以数据库没有任何变化。
我使用错误的 URL 将其上传到 Iris 还是代码有问题?我是 Android 开发的新手,非常感谢您的帮助,因为我已经为此苦苦挣扎了好几天。