简体   繁体   English

从android发送帖子请求

[英]sending a post request from android

I have been struggling for hours now. 我已经奋斗了几个小时。 I have been trying to make a post request from my android application. 我一直在尝试从我的android应用程序发出发布请求。 I figured out that i can't just make a request from the main thread, so i found out that i can put it in a thread runnable. 我发现我不能只从主线程发出请求,所以我发现我可以将其放在可运行的线程中。 I snooped around for answers tried out a lot of methods people used, that worked for others and none of them worked for me. 我偷偷地寻找答案,尝试了很多人使用的方法,这些方法对别人有用,但对我来说却没有。 The one I kind of understood was this one(doesn't work for me): 我所理解的是这个(对我不起作用):

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();

            }
    }
});
}

I allowed it to get acces to internet so: 我允许它访问互联网,所以:

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

The server side script api works, as i am just using it in a web app. 服务器端脚本api可以正常工作,因为我只是在Web应用程序中使用它。

Can somebody point me in the right direction? 有人可以指出我正确的方向吗? I am clueless. 我无能为力。 Where could be the error? 错误可能在哪里? Perhaps the error is in my gradle?(I had to download the http libraries from apache.com and include the in my gradle - had to add a dependency compile 'org.apache.httpcomponents:httpclient:4.5' and a bunch of packagingOptions { (我必须从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'

})Is there an easy way? })有没有简单的方法? Also found out about the okhttp library, I'm going to try to implement it, but i'm really not good with gradle and importing foreign libraries. 还发现了有关okhttp库的信息,我将尝试实现它,但是我对gradle和导入外部库确实不满意。

This is what i get: 这就是我得到的:

 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:: org.apache.http.message.BasicHttpResponse@417e0af0

Please help. 请帮忙。 :) All the help is very appreciated. :)所有帮助非常感谢。

Not sure about your error. 不确定您的错误。
But for your question : 但对于您的问题:

Is there an easy way? 有没有简单的方法? Also found out about the okhttp library, 还发现了有关okhttp库的信息,

Comparison of Android Networking Libraries: OkHTTP, Retrofit, Volley is the beautiful post that compares all the available network libraries on every factor. Android网络库的比较:OkHTTP,Retrofit,Volley是一本很漂亮的文章,它比较了每个因素上的所有可用网络库。 Never think of framing the headers, body and parsing the responses on your own. 永远不要考虑自己构造标题,正文和解析响应。 Also retry, cancel-ongoing-requests, caching etc all are very easily done through libraries. 重试,取消正在进行的请求,缓存等也都可以通过库轻松完成。 And Retrofit tops on every factor. 而翻新在各个方面都至关重要。 We are using it and it is good. 我们正在使用它,这很好。 It supports JSON, XML, Jackson, Moshi, Protobuf and also Wire data formats. 它支持JSON,XML,Jackson,Moshi,Protobuf以及Wire数据格式。 All you need to do is to create request Body model classes, and response models. 您需要做的就是创建请求主体模型类和响应模型。 Take a look at Retrofit beginner doc . 看一看Retrofit入门文档

OK. 好。 First of all thank you all for answering. 首先,谢谢大家的回答。 :) It helped a lot. :)很有帮助。 I just imported the okhttp library into the project and i will work with this from now on. 我刚刚将okhttp库导入到项目中,从现在开始我将使用它。 Seems easy enough. 似乎很容易。 Thanks again. 再次感谢。

May the code be with you all! 愿代码与大家同在!

Sincerely, Matic 顺祝商祺

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM