我一直在努力工作几个小时。我一直试图从我的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正常工作,因为我只是在Web应用程序中使用它。
有人能指出我正确的方向吗?我很无能为力。哪里可能是错误?也许错误在我的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:: org.apache.http.message.BasicHttpResponse@417e0af0
请帮忙。 :) 非常感谢所有的帮助。
答案 0 :(得分:0)
不确定您的错误 但是对于你的问题:
有简单的方法吗?还发现了okhttp库,
Comparison of Android Networking Libraries: OkHTTP, Retrofit, Volley是一个漂亮的帖子,它比较了每个因素上的所有可用网络库。永远不要想到自己构建标题,正文和解析响应。还可以通过库轻松完成重试,取消 - 持续请求,缓存等。而且Retrofit在每个因素上都是最重要的。我们正在使用它并且很好。它支持JSON,XML,Jackson,Moshi,Protobuf以及Wire数据格式。您需要做的就是创建请求Body模型类和响应模型。看看Retrofit beginner doc。
答案 1 :(得分:0)
行。首先,谢谢大家的回答。 :)它帮助了很多。我刚刚将okhttp库导入到项目中,从现在开始我将使用它。看起来很简单。再次感谢。
愿代码与您同在!
此致 MATIC