我想从Android输入文本到网站中,并且我读到了httppost是一个不错的选择。我下载HttpClient 4.2.2(GA)tar.gz,解压缩它们,并将7个jar文件复制到Eclipse中我的android项目的lib文件夹中。我很确定我拿到了所有的罐子,因为它们与网站上列出的那些相匹配。在Android上使用HttpPost的错误
我接着就从复制并粘贴顶部教程:http://hc.apache.org/httpcomponents-client-ga/quickstart.html
我进口的一切,留下了这个错误:
EntityUtils.consume(entity1); //X
} finally {
httpGet.releaseConnection(); //X
的这部分程序是在教程中两个地方,并且在两者都发生错误。 Eclipse中说,对第一行:
"The method consume(HttpEntity) is undefined for the type EntityUtils."
下联:
"The method releaseConnection() is undefined for the type HttpGet."
我敢肯定,我下载的每一个罐子,正确运输它们,进口一切。什么是造成错误?谢谢。
这是我现在拥有的。爱德华,我使用了你方法中的一些代码,但只是把它们放到onCreate中。但是,这不起作用。在我从前一个活动转到这个活动后几秒钟,我收到消息说应用程序“意外停止”。
我有一个关于将我的字符串输入到网站文本字段的问题:我是否使用HttpParams的NameValuePairs?这是我的代码,你能看到有什么问题吗?谢谢。
package com.example.myapp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;
public class BalanceCheckerActivity extends Activity {
private final String LOGIN_URL = "https://someloginsite.com"; //username and password
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balance_checker);
String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");
//Building post parameters, key and value pair
List<NameValuePair> accountInfo = new ArrayList<NameValuePair>(2);
accountInfo.add(new BasicNameValuePair("inputEnterpriseId", username));
accountInfo.add(new BasicNameValuePair("password", password));
//Creating HTTP client
HttpClient httpClient = new DefaultHttpClient();
//Creating HTTP Post
HttpPost httpPost = new HttpPost(LOGIN_URL);
BasicHttpParams params = new BasicHttpParams();
params.setParameter("inputEnterpriseID", username);
params.setParameter("password", password);
httpPost.setParams(params);
//Url Encoding the POST parameters
try {
httpPost.setEntity(new UrlEncodedFormEntity(accountInfo));
}
catch (UnsupportedEncodingException e) {
// writing error to Log
e.printStackTrace();
startActivity(new Intent(this, AccountInputActivity.class));
}
HttpResponse response = null;
InputStreamReader iSR = null;
String source = null;
// Making HTTP Request
try {
response = httpClient.execute(httpPost);
// writing response to log
Log.d("Http Response:", response.toString());
iSR = new InputStreamReader(response.getEntity().getContent());
BufferedReader br = new BufferedReader(iSR);
source = "";
while((source = br.readLine()) != null)
{
source += br.readLine();
}
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
startActivity(new Intent(this, AccountInputActivity.class));
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
startActivity(new Intent(this, AccountInputActivity.class));
}
System.out.println(source);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_balance_checker, menu);
return true;
}
}
在您选择仍然在使用来自Android的SDK库,因为它USIG相同的包名称...尝试http://code.google.com/p/httpclientandroidlib/或由于Android没有从Apache的使用文档已经事实有这个库,但早期版本 – Selvin
为什么你用两种不同的方式设置两次params? – MrHIDEn