没有在MongoDB[ANDROID]中进行插入
我正在尝试将数据从Android应用程序插入MongoDB。为此,我编写了这段代码(如下所示),但在执行insert任务时,这段代码不起任何作用。此代码既不会给出任何错误或异常,也不会向数据库插入数据。那么问题是什么?我如何解决这个问题 在SetNotification.java中,我执行以下操作:没有在MongoDB[ANDROID]中进行插入,android,mongodb,android-studio,Android,Mongodb,Android Studio,我正在尝试将数据从Android应用程序插入MongoDB。为此,我编写了这段代码(如下所示),但在执行insert任务时,这段代码不起任何作用。此代码既不会给出任何错误或异常,也不会向数据库插入数据。那么问题是什么?我如何解决这个问题 在SetNotification.java中,我执行以下操作: Task save_task = new Task(); save_task.date = showDate.getText().toString(); save_task.location = s
Task save_task = new Task();
save_task.date = showDate.getText().toString();
save_task.location = showLocation.getText().toString();
save_task.description = note.getText().toString();
SaveAsyncTask tsk = new SaveAsyncTask(SetNotification.this);
tsk.execute(save_task);
这是SaveAsyncTask.jav一个类:
package com.example.abc.project1.MongoHQ;
/**
* Created by abc on 02-May-16.
*/
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import com.example.abc.project1.SetNotification;
import com.example.abc.project1.Task;
public class SaveAsyncTask extends AsyncTask<Task, Void, Boolean> {
private static Context context;
public SaveAsyncTask(Context c) {
context = c;
}
@Override
protected Boolean doInBackground(Task... arg0) {
try
{
Task task = arg0[0];
QueryBuilder qb = new QueryBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(qb.buildContactsSaveURL());
StringEntity params =new StringEntity(qb.createTask(task));
request.addHeader("content-type", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode()<205)
{
Toast.makeText(context, "SAVED", Toast.LENGTH_SHORT).show();
return true;
}
else
{
Toast.makeText(context, "Saving failed", Toast.LENGTH_SHORT).show();
return false;
}
} catch (Exception e) {
//Log.e("MYAPP", "exception",e);
e.printStackTrace();
return false;
}
}
}
package com.example.abc.project1.MongoHQ;
import com.example.abc.project1.Task;
/**
* Created by abc on 02-May-16.
*/
public class QueryBuilder {
/**
* Specify your database name here
* @return
*/
public String getDatabaseName() {
return "location_notification";
}
/**
* Specify your MongoLab API here
* @return
*/
public String getApiKey() {
return "################################";
}
/**
* This constructs the URL that allows you to manage your database,
* collections and documents
* @return
*/
public String getBaseUrl()
{
return "https://api.mongolab.com/api/1/databases/"+getDatabaseName()+"/collections/";
}
/**
* Completes the formating of your URL and adds your API key at the end
* @return
*/
public String docApiKeyUrl()
{
return "?apiKey="+getApiKey();
}
/**
* Returns the docs101 collection
* @return
*/
public String documentRequest()
{
return "tasks";
}
/**
* Builds a complete URL using the methods specified above
* @return
*/
public String buildContactsSaveURL()
{
return getBaseUrl()+documentRequest()+docApiKeyUrl();
}
/**
* Formats the contact details for MongoHQ Posting
* @param : Details of the person
* @return
*/
public String createTask(Task task)
{
return String
.format("{\"document\" : {\"date\": \"%s\", "
+ "\"location\": \"%s\", \"description\": \"%s\"}, \"safe\" : true}",
task.date, task.location, task.description);
}
}