我目前正在开发一款应用程序,在向我的Iris CouchDb上传JSONObject时遇到了问题。但是我不能让它工作。
这是我现在使用的代码:
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开发的新手,真的很感谢你的帮助,因为我已经在这个问题上挣扎了几天。
发布于 2015-06-25 17:49:39
也许您需要使用https://accountname.iriscouch.com/dbname而不是https://dbname.iriscouch.com/dbname。
此外,如果请求抛出一个IOException,你的应用程序将会默默地接受错误,所以你可能看不到它。
https://stackoverflow.com/questions/30831979
复制相似问题