2015-11-28 157 views
0

我一直在掙扎幾個小時。我一直在試圖從我的android應用程序發出一個請求。我發現我不能只從主線程發出請求,所以我發現我可以將它放在可運行的線程中。我嘗試了很多人使用的方法,爲別人工作,並且沒有人爲我工作。我有點明白了一個爲這一個(不爲我工作):我允許它獲得的存取權限互聯網這樣發送android的帖子請求

package com.example.matt.event; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 
import java.util.List; 


public class lokacije extends AppCompatActivity { 

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

Thread thread = new Thread(new Runnable(){ 
    @Override 
    public void run() 
    { 
      HttpClient httpClient = new DefaultHttpClient(); 
      // replace with your url 
      HttpPost httpPost = new HttpPost("http://www.awesomeholidays.com/login.php"); 


      //Post Data 
      List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(2); 
      nameValuePair.add(new BasicNameValuePair("username", "123")); 
      nameValuePair.add(new BasicNameValuePair("password", "456")); 


      //Encoding POST data 
      try { 
       httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
      } catch (UnsupportedEncodingException e) { 
       Log.d("omg it doesn't work",e.toString()); 
       e.printStackTrace(); 
      } 

      //making POST request. 
      try { 
       HttpResponse response = httpClient.execute(httpPost); 
       // write response to log 
       Log.d("Http Post Response:", response.toString()); 
      } catch (ClientProtocolException e) { 
       // Log exception 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // Log exception 
       e.printStackTrace(); 

      } 
    } 
}); 
} 

<uses-permission android:name="android.permission.INTERNET"/> 

服務器端腳本API作品,我只是在一個網絡應用程序中使用它。

有人可以指出我正確的方向嗎?我無能爲力。哪裏可能是錯誤?也許錯誤在我的gradle中(我必須從apache.com下載http庫幷包含在我的gradle中 - 必須添加一個依賴編譯org.apache.httpcomponents:httpclient:4.5'和一堆packagingOptions {

exclude 'META-INF/DEPENDENCIES' 

    exclude 'META-INF/NOTICE' 

    exclude 'META-INF/LICENSE' 

    exclude 'META-INF/LICENSE.txt' 

    exclude 'META-INF/NOTICE.txt' 

})有沒有簡單的方法?也發現了關於okhttp庫,我將嘗試實現它,但我真的不擅長使用gradle和導入外部庫。

這就是我得到:

11-28 20:49:49.587 10450-10450/? D/dalvikvm: Late-enabling CheckJNI 
11-28 20:49:50.008 10450-10450/com.example.matic.eventer W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
11-28 20:49:50.008 10450-10450/com.example.matic.eventer I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested 
11-28 20:49:50.008 10450-10450/com.example.matic.eventer W/dalvikvm: VFY: unable to resolve interface method 14051: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
11-28 20:49:50.008 10450-10450/com.example.matic.eventer D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
11-28 20:49:50.018 10450-10450/com.example.matic.eventer I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode 
11-28 20:49:50.018 10450-10450/com.example.matic.eventer W/dalvikvm: VFY: unable to resolve interface method 14055: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
11-28 20:49:50.018 10450-10450/com.example.matic.eventer D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002 
11-28 20:49:50.238 10450-10450/com.example.matic.eventer I/dalvikvm: Could not find method android.view.ViewGroup.onRtlPropertiesChanged, referenced from method android.support.v7.widget.Toolbar.onRtlPropertiesChanged 
11-28 20:49:50.248 10450-10450/com.example.matic.eventer W/dalvikvm: VFY: unable to resolve virtual method 13952: Landroid/view/ViewGroup;.onRtlPropertiesChanged (I)V 
11-28 20:49:50.248 10450-10450/com.example.matic.eventer D/dalvikvm: VFY: replacing opcode 0x6f at 0x0007 
11-28 20:49:50.268 10450-10450/com.example.matic.eventer I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 
11-28 20:49:50.268 10450-10450/com.example.matic.eventer W/dalvikvm: VFY: unable to resolve virtual method 402: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
11-28 20:49:50.268 10450-10450/com.example.matic.eventer D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002 
11-28 20:49:50.278 10450-10450/com.example.matic.eventer I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 
11-28 20:49:50.278 10450-10450/com.example.matic.eventer W/dalvikvm: VFY: unable to resolve virtual method 424: Landroid/content/res/TypedArray;.getType (I)I 
11-28 20:49:50.278 10450-10450/com.example.matic.eventer D/dalvikvm: VFY:replacing opcode 0x6e at 0x0002 
11-28 20:49:50.438 10450-10450/com.example.matic.eventer D/libEGL: loaded /system/lib/egl/libEGL_mali.so 
11-28 20:49:50.458 10450-10450/com.example.matic.eventer D/libEGL: loaded /system/lib/egl/libGLESv1_CM_mali.so 
11-28 20:49:50.458 10450-10450/com.example.matic.eventer D/libEGL: loaded /system/lib/egl/libGLESv2_mali.so 
11-28 20:49:50.498 10450-10454/com.example.matic.eventer D/dalvikvm: GC_CONCURRENT freed 198K, 9% free 9534K/10375K, paused 3ms+32ms, total 128ms 
11-28 20:49:50.518 10450-10450/com.example.matic.eventer D/OpenGLRenderer: Enabling debug mode 0 
11-28 20:49:54.722 10450-10450/com.example.matic.eventer E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
11-28 20:49:54.722 10450-10450/com.example.matic.eventer E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length 
11-28 20:49:56.514 10450-10583/com.example.matic.eventer D/Http Post Response:: [email protected] 

請幫助。 :) 所有的幫助非常感謝。

+0

入住這http://stackoverflow.com/questions/2938502/sending-post-data-in-android http://stackoverflow.com/questions/31552242/sending-http- post-request-with-android – RAP

+0

瞭解volley library [in this awesome tutorial!](http://arnab.ch/blog/2013/08/asynchronous-http-requests-in-android-using-volley/)It會讓你的生活更輕鬆。 :) – rafid059

回答

0

不確定你的錯誤。
但對於你的問題:

有一個簡單的方法是什麼?還發現了關於okhttp庫,

Comparison of Android Networking Libraries: OkHTTP, Retrofit, Volley是一個美麗的職位,比較所有可用的網絡庫上的每個因素。永遠不要想到自己構造標題,正文和解析響應。同樣重試,取消正在進行的請求,緩存等都可以通過庫很容易地完成。並改進每一個因素。我們正在使用它,它很好。它支持JSON,XML,Jackson,Moshi,Protobuf和Wire數據格式。你所需要做的就是創建請求體模型類和響應模型。看看Retrofit beginner doc

0

好的。首先感謝大家回答。 :)它幫了很多。我只是將okhttp庫導入到項目中,從現在開始我將繼續使用它。看起來很簡單。再次感謝。

願代碼與你同在!

真誠, 馬蒂奇