2016-08-18 117 views
1

我正在創建一個用於將Json文件發送到我創建的本地數據庫的Android應用程序。但是,每當我嘗試啓動它,我的gradle同步失敗。我把正確的位置設置在正確的位置(我認爲),但我不知道爲什麼。 這裏是我的gradle產出:從Android應用程序發送Json到本地數據庫

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> 

我必須放置依賴於另一個地方?謝謝

編輯:這是我的發送

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; 

    } 
} 
+0

如果您使用的是Windows,那麼最好將您的項目放在其他目錄上,因爲窗口試圖保護C盤。問題可能是org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler由於安全原因無法訪問構建文件。 –

回答

0

首先,你的代碼,你發送JSON的一部分份額代碼。

二,爲什麼你使用這麼多的依賴關係?也許你正在嘗試使用Apache類,並在互聯網的某個地方找到這些依賴關係。完整的解決方案可以在您發佈代碼後提供。但嘗試刪除此行:classpath 'com.android.tools.build:gradle:2.1.3'。並且使用HttpURLConnection而不是Apache類,因爲最後不推薦使用。

(我知道這必須是一條評論,但我的聲望很低)。

+0

編輯。此外,我試圖刪除所有這些,但我不能使用它 – lavendemmia

+0

你有超過50的聲望,所以你應該能夠評論問題,Turkhan。 – halfer

0
classpath 'com.android.tools.build:gradle:2.1.3' 

它應該在您的項目gradle文件中給出,而不是在模塊gradle文件中給出。

爲Apache HTTP客戶端使用,添加

useLibrary 'org.apache.http.legacy'

模塊gradle這個我android的標籤內。 e之後

buildToolsVersion "24.0.1" 
+0

現在我刪除了該文件,並且無法找到我的代碼中的所有httpclient,httppost,ecc ecc。我如何解決這個問題? – lavendemmia

相關問題