2016-03-25 34 views
-1

我有一些在AndroidStudio中使用Apache Http的困難。Android工作室不會http和NetBeans做

搜索了一遍,嘗試了很多,並且也使用NetBeans進行了測試。

最終我最終以 https://hc.apache.org/downloads.cgi(4.5.2.zip)手動下載庫。添加這些庫到NetBeans測試項目,並使用此代碼它工作正常(在NetBeans):從API

CloseableHttpClient client = HttpClients.createDefault(); 
HttpPost httpPost = new HttpPost("http://localhost:5555/test"); 

List<NameValuePair> params = new ArrayList<>(); 
params.add(new BasicNameValuePair("username", "Jim")); 
params.add(new BasicNameValuePair("password", "password")); 
httpPost.setEntity(new UrlEncodedFormEntity(params)); 

CloseableHttpResponse response = client.execute(httpPost); 

響應消息:

recived request 

在AndroidStudio這一點兒也不似乎工作,我只是可以找出原因。我在/ libs文件夾中添加了這些相同的庫。 我已經找到了一些竅門,我需要在我的應用程序的gradle添加文件,所以我結束了與此:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 23 
    buildToolsVersion "23.0.2" 

    defaultConfig { 
     applicationId "com.example.jim.app" 
     minSdkVersion 16 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
    // useLibrary 'org.apache.http.legacy' 

    packagingOptions { 
     exclude 'META-INF/DEPENDENCIES' 
     exclude 'META-INF/NOTICE' 
     exclude 'META-INF/LICENSE' 
     exclude 'META-INF/LICENSE.txt' 
     exclude 'META-INF/NOTICE.txt' 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:23.2.0' 
} 

這是編譯和仿真器運行良好。但是當我使用完全相同的代碼發送一個HTTP POST現在我得到這個錯誤是什麼沒有任何意義,我:

E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.jim.app, PID: 2303 
    java.lang.IllegalStateException: Could not execute method for android:onClick 

(再次單擊工作正常..)

如果有些確實知道我錯過了我的建議。 另外,如果需要更多信息,我很樂意證明更多。

編輯 整個Java在Android中

文件
public class SignInActivity extends AppCompatActivity { 
    private final String API_URL = "http://localhost:5555/user"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_sign_in); 
    } 

    /** Authenticate user */ 
    public void signIn(View view) { 

     // Test http post request 
     try { 
      CloseableHttpClient client = HttpClients.createDefault(); 
      HttpPost httpPost = new HttpPost(API_URL); 

      List<NameValuePair> params = new ArrayList<>(); 
      params.add(new BasicNameValuePair("username", "John")); 
      params.add(new BasicNameValuePair("password", "pass")); 
      httpPost.setEntity(new UrlEncodedFormEntity(params)); 

      CloseableHttpResponse response = client.execute(httpPost); 
      client.close(); 
     } catch (ClientProtocolException e) { 
      System.err.println(e.getMessage()); 
     } catch (IOException e) { 
      System.err.println(e.getMessage()); 
     } 
    } 

    /** Navigate to activity Sign Up */ 
    public void signUp(View view) { 
     Intent intent = new Intent(this, SignUpActivity.class); 
     startActivity(intent); 
    } 
} 
+0

我們可以看到你的方法'onClick'和你附加他們在哪裏? –

+0

這整個問題**沒有意義**。 Android Studio是一個IDE。您是否試圖爲Android編程?如果是這樣,'localhost'永遠不會工作,除非你設法在你的android設備上創建一個服務器。 – 323go

+0

328go這就是爲什麼你downvote? 我知道Android Studio是一個IDE,而我是在Android上編程的。我有一個本地Api在線連接mongo,我不知道爲什麼這對你來說很奇怪? –

回答

-1

如果您使用API​​ 23,你應該做的像以下使用Apache庫。

defaultConfig { 
     applicationId "com.example.jim.app" 
     minSdkVersion 16 
     targetSdkVersion 23 
     versionCode 1 
     versionName "1.0" 
     useLibrary 'org.apache.http.legacy' 
    } 
+0

這是否會替換我的庫或只需要添加? –

+0

只是你必須添加useLibrary'org.apache.http.legacy' – user3148900

+0

這似乎並不工作 –