Java 安卓工作室赢得';http和NetBeans没有
我在AndroidStudio中使用apache Http时遇到了一些困难 到处搜索,尝试了很多,还用NetBeans进行了测试 最终,我在网站上手动下载了libs (4.5.2.zip)。将这些lib添加到Netbeans测试项目中,使用这段代码可以正常工作(在Netbeans中): 在AndroidStudio中,这似乎不起作用,我只能找出原因。我在/libs文件夹中添加了相同的lib。 我已经找到了一些需要添加到gradle应用程序文件中的提示,因此我最终得出以下结论:Java 安卓工作室赢得';http和NetBeans没有,java,android,apache-httpclient-4.x,Java,Android,Apache Httpclient 4.x,我在AndroidStudio中使用apache Http时遇到了一些困难 到处搜索,尝试了很多,还用NetBeans进行了测试 最终,我在网站上手动下载了libs (4.5.2.zip)。将这些lib添加到Netbeans测试项目中,使用这段代码可以正常工作(在Netbeans中): 在AndroidStudio中,这似乎不起作用,我只能找出原因。我在/libs文件夹中添加了相同的lib。 我已经找到了一些需要添加到gradle应用程序文件中的提示,因此我最终得出以下结论: apply plu
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
(另一次单击可以正常工作.)
如果有人知道我遗漏了什么,我愿意接受建议。
另外,如果需要更多的信息,我很乐意提供更多
编辑
Android中的整个java文件
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);
}
}
公共类的重要性扩展了AppCompatActivity{
私有最终字符串API_URL=”http://localhost:5555/user";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u sign\u in);
}
/**验证用户*/
公共无效登录(查看){
//测试http post请求
试一试{
CloseableHttpClient=HttpClients.createDefault();
HttpPost HttpPost=新的HttpPost(API_URL);
List params=new ArrayList();
添加参数(新的BasicNameValuePair(“用户名”、“约翰”));
参数添加(新的BasicNameValuePair(“密码”、“通行证”);
setEntity(新的UrlEncodedFormEntity(参数));
CloseableHttpResponse response=client.execute(httpPost);
client.close();
}捕获(客户端协议例外e){
System.err.println(e.getMessage());
}捕获(IOE异常){
System.err.println(e.getMessage());
}
}
/**导航到活动注册*/
公共无效注册(视图){
意向意向=新意向(此为SignUpActivity.class);
星触觉(意向);
}
}
如果您使用API23,您应该像下面一样使用apache库
defaultConfig {
applicationId "com.example.jim.app"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
useLibrary 'org.apache.http.legacy'
}
我们可以看看你的方法
onClick
吗?你把它们放在哪里了?这个问题毫无意义。Android Studio是一个IDE。你想为Android编程吗?如果是这样的话,localhost
将永远无法工作,除非您在android设备上创建了一个服务器。328go这就是您否决投票的原因?我知道Android Studio是一个IDE,我正在Android上编程。我有一个本地的Api在线连接mongo,我不知道为什么这对你来说很奇怪?Raphal Teyssandier,我会更新我的问题为什么你不使用网络库?Google对Volley的改进。这会取代我的libs吗?还是我只需要添加这个?你只需要添加useLibrary'org.apache.http.legacy'就可以清楚地告诉我Android studio错误日志吗?使用这个,我需要使用一组只抛出异常null的其他方法仅此而已..您可以尝试直接在URL中传递参数“”
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);
}
}
defaultConfig {
applicationId "com.example.jim.app"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
useLibrary 'org.apache.http.legacy'
}