我正在尝试将文本从 Android 输入到网站中,并且我读到 httppost 是一个不错的选择。我下载了 HttpClient 4.2.2 (GA) tar.gz,解压缩它们,然后将 7 个 jars 复制到 Eclipse 中我的 android 项目的 lib 文件夹中。我很确定我得到了所有的罐子,因为它们与网站上列出的那些相匹配。
然后我继续从以下位置复制并粘贴顶级教程: http: //hc.apache.org/httpcomponents-client-ga/quickstart.html
我导入了所有内容,并留下了这个错误:
EntityUtils.consume(entity1); //X
} finally {
httpGet.releaseConnection(); //X
这部分代码位于教程中的两个地方,并且两个地方都会出现错误。Eclipse 说第一行:
“EntityUtils 类型的方法 consume(HttpEntity) 未定义。”
第二行:
“对于 HttpGet 类型,方法 releaseConnection() 未定义。”
我很确定我下载了每个 jar,正确传输它们,并导入了所有内容。是什么造成了错误?谢谢。
这就是我现在所拥有的。爱德华,我使用了您方法中的一些代码,但只是将它们放入 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;
}
}