Java 从android应用程序向本地数据库发送Json
我正在创建一个android应用程序,用于将json文件发送到我创建的本地数据库。但是,每当我尝试启动它时,我的gradle同步就失败了。我想我把一切都安排在了正确的地方,但我不知道为什么。 这是我的格拉德尔:Java 从android应用程序向本地数据库发送Json,java,android,json,database,gradle,Java,Android,Json,Database,Gradle,我正在创建一个android应用程序,用于将json文件发送到我创建的本地数据库。但是,每当我尝试启动它时,我的gradle同步就失败了。我想我把一切都安排在了正确的地方,但我不知道为什么。 这是我的格拉德尔: apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion "24.0.1" defaultConfig { applicat
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "francesco.postjson"
minSdkVersion 23
targetSdkVersion 24
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
classpath 'com.android.tools.build:gradle:2.1.3'
compile 'cz.msebera.android:httpclient:4.4.1.1'
compile files('libs/httpcore-4.4.4.jar')
compile files('libs/okhttp-3.4.1.jar')
compile files('libs/commons-codec-1.9.jar')
compile files('libs/commons-logging-1.2.jar')
compile 'com.squareup.okhttp:okhttp:2.0.0'
compile files('libs/httpclient-win-4.5.2.jar')
compile files('libs/httpclient-4.5.2.jar')
compile files('libs/httpcore-4.4.4.jar')
compile files('libs/fluent-hc-4.5.2.jar')
}
我正在使用这些类型的依赖项,但出现了以下错误:
Error:(25, 0) Could not find method classpath() for arguments [com.android.tools.build:gradle:2.1.3] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
<a href="openFile:C:\Users\Francesco\AndroidStudioProjects\POSTJSON\app\build.gradle">Open File</a>
错误:(25,0)在org.gradle.api.internal.artifacts.dsl.dependency.DefaultDependencyHandler类型的对象上找不到参数[com.android.tools.build:gradle:2.1.3]的方法classpath()。
我必须把依赖项放在另一个地方吗?多谢各位
编辑:这是我的发送代码
package francesco.postjson;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLConnection;
import org.json.JSONObject;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import francesco.postjson.Person;
public class MainActivity extends Activity implements OnClickListener {
TextView tvIsConnected;
EditText etName,etCountry,etTwitter;
Button btnPost;
Person person;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get reference to the views
tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
etName = (EditText) findViewById(R.id.etName);
etCountry = (EditText) findViewById(R.id.etCountry);
etTwitter = (EditText) findViewById(R.id.etTwitter);
btnPost = (Button) findViewById(R.id.btnPost);
// check if you are connected or not
if(isConnected()){
tvIsConnected.setBackgroundColor(0xFF00CC00);
tvIsConnected.setText("You are conncted");
}
else{
tvIsConnected.setText("You are NOT conncted");
}
// add click listener to Button "POST"
btnPost.setOnClickListener(this);
}
public static String POST(String url, Person person){
InputStream inputStream = null;
String result = "";
try {
// 1. create HttpClient
HttpClient httpclient = new DefaultHttpClient();
// 2. make POST request to the given URL
HttpPost httpPost = new HttpPost(url);
String json = "";
// 3. build jsonObject
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("name", person.getName());
jsonObject.accumulate("country", person.getCountry());
jsonObject.accumulate("twitter", person.getTwitter());
// 4. convert JSONObject to JSON to String
json = jsonObject.toString();
// ** Alternative way to convert Person object to JSON string usin Jackson Lib
// ObjectMapper mapper = new ObjectMapper();
// json = mapper.writeValueAsString(person);
// 5. set json to StringEntity
StringEntity se = new StringEntity(json);
// 6. set httpPost Entity
httpPost.setEntity(se);
// 7. Set some headers to inform server about the type of the content
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
// 8. Execute POST request to the given URL
HttpResponse httpResponse = httpclient.execute(httpPost);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
// 10. convert inputstream to string
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return result;
}
public boolean isConnected(){
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
return true;
else
return false;
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.btnPost:
if(!validate())
Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
// call AsynTask to perform network operation on separate thread
new HttpAsyncTask().execute("http://hmkcode.appspot.com/jsonservlet");
break;
}
}
private class HttpAsyncTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
person = new Person();
person.setName(etName.getText().toString());
person.setCountry(etCountry.getText().toString());
person.setTwitter(etTwitter.getText().toString());
return POST(urls[0],person);
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(String result) {
Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
}
}
private boolean validate(){
if(etName.getText().toString().trim().equals(""))
return false;
else if(etCountry.getText().toString().trim().equals(""))
return false;
else if(etTwitter.getText().toString().trim().equals(""))
return false;
else
return true;
}
private static String convertInputStreamToString(InputStream inputStream) throws IOException{
BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
String line = "";
String result = "";
while((line = bufferedReader.readLine()) != null)
result += line;
inputStream.close();
return result;
}
}
package francesco.postjson;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.net.URLConnection;
导入org.json.JSONObject;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.app.Activity;
导入francesco.postjson.Person;
公共类MainActivity扩展活动实现OnClickListener{
文本视图tv断开连接;
EditText-etName,etCountry,EtWitter;
按钮btnPost;
个人;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取对视图的引用
tvIsConnected=(TextView)findViewById(R.id.tvIsConnected);
etName=(EditText)findViewById(R.id.etName);
etCountry=(EditText)findViewById(R.id.etCountry);
etTwitter=(EditText)findViewById(R.id.etTwitter);
btnPost=(按钮)findViewById(R.id.btnPost);
//检查您是否已连接
如果(断开连接()){
t断开连接。设置背景颜色(0xFF00CC00);
tvIsConnected.setText(“您已连接”);
}
否则{
tvIsConnected.setText(“您未连接”);
}
//将click listener添加到按钮“POST”
btnPost.setOnClickListener(此);
}
公共静态字符串帖子(字符串url,Person){
InputStream InputStream=null;
字符串结果=”;
试一试{
//1.创建HttpClient
HttpClient HttpClient=新的DefaultHttpClient();
//2.向给定URL发出POST请求
HttpPost HttpPost=新的HttpPost(url);
字符串json=“”;
//3.构建jsonObject
JSONObject JSONObject=新的JSONObject();
累积(“name”,person.getName());
jsonObject.acculate(“country”,person.getCountry());
累积(“twitter”,person.getTwitter());
//4.将JSONObject转换为JSON转换为字符串
json=jsonObject.toString();
//**在Jackson库中将Person对象转换为JSON字符串的替代方法
//ObjectMapper mapper=新的ObjectMapper();
//json=mapper.writeValueAsString(person);
//5.将json设置为StringEntity
StringEntity se=新的StringEntity(json);
//6.设置httpPost实体
httpPost.setEntity(se);
//7.设置一些标题以通知服务器内容的类型
setHeader(“接受”、“应用程序/json”);
setHeader(“内容类型”、“应用程序/json”);
//8.对给定URL执行POST请求
HttpResponse HttpResponse=httpclient.execute(httpPost);
//9.将响应作为inputStream接收
inputStream=httpResponse.getEntity().getContent();
//10.将inputstream转换为字符串
如果(inputStream!=null)
结果=convertInputStreamToString(inputStream);
其他的
结果=“不起作用!”;
}捕获(例外e){
d(“InputStream”,例如getLocalizedMessage());
}
//11.返回结果
返回结果;
}
公共布尔值未连接(){
ConnectivityManager connMgr=(ConnectivityManager)getSystemService(Activity.CONNECTIVITY_服务);
NetworkInfo NetworkInfo=connMgr.getActiveNetworkInfo();
if(networkInfo!=null&&networkInfo.isConnected())
返回true;
其他的
返回false;
}
@凌驾
公共void onClick(视图){
开关(view.getId()){
案例R.id.btnPost:
如果(!validate())
Toast.makeText(getBaseContext(),“输入一些数据!”,Toast.LENGTH_LONG.show();
//调用AsynTask在单独的线程上执行网络操作
新建HttpAsyncTask()。执行(“http://hmkcode.appspot.com/jsonservlet");
打破
}
}
私有类HttpAsyncTask扩展了AsyncTask{
@凌驾
受保护的字符串doInBackground(字符串…URL){
person=新的person();
person.setName(etName.getText().toString());
person.setCountry(etCountry.getText().toString());
setTwitter(etTwitter.getText().toString());
回帖(URL[0],人);
}
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(字符串结果){
Toast.makeText(getBaseContext(),“已发送数据!”,Toast.LENGTH_LONG.show();
}
}
私有布尔验证(){
if(etName.getText().toString().trim().equals(“”)
返回false;
else if(etCountry.getText().toString().trim().equals(“”)
返回false;
else if(etTwitter.getText().toString().trim().equals(“”)
classpath 'com.android.tools.build:gradle:2.1.3'
buildToolsVersion "24.0.1"