android eclipse针对web服务器设置http POST和GET
我需要帮助在android中使用eclipse设置应用程序 我以前没有在JAVA开发中使用eclipse,所以对于如何将这一切与android结合在一起,我有点生疏 我下载了一个脚本,用我的web服务器进行测试,该脚本被设置为在访问时生成输出(此时为JSON命名的值对) 我正在使用-android eclipse针对web服务器设置http POST和GET,android,eclipse,apache,http,Android,Eclipse,Apache,Http,我需要帮助在android中使用eclipse设置应用程序 我以前没有在JAVA开发中使用eclipse,所以对于如何将这一切与android结合在一起,我有点生疏 我下载了一个脚本,用我的web服务器进行测试,该脚本被设置为在访问时生成输出(此时为JSON命名的值对) 我正在使用- 面向Java开发人员的Eclipse IDE 版本:Helios Service Release 2 构建id:20110218-0911 Android开发工具包 版本:10.0.1.v201103111512-
面向Java开发人员的Eclipse IDE
版本:Helios Service Release 2
构建id:20110218-0911 Android开发工具包
版本:10.0.1.v201103111512-110841 到目前为止的JAVA代码-
package new.android.test;
import android.app.Activity;
import android.os.Bundle;
import java.io.ByteArrayInputStream;
import java.net.Socket;
import org.apache.http.ConnectionReuseStrategy;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.DefaultConnectionReuseStrategy;
import org.apache.http.impl.DefaultHttpClientConnection;
import org.apache.http.message.BasicHttpEntityEnclosingRequest;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.params.SyncBasicHttpParams;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.ExecutionContext;
import org.apache.http.protocol.HttpProcessor;
import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.protocol.ImmutableHttpProcessor;
import org.apache.http.protocol.RequestConnControl;
import org.apache.http.protocol.RequestContent;
import org.apache.http.protocol.RequestExpectContinue;
import org.apache.http.protocol.RequestTargetHost;
import org.apache.http.protocol.RequestUserAgent;
import org.apache.http.util.EntityUtils;
/**
* Elemental example for executing a POST request.
* <p>
* Please note the purpose of this application is demonstrate the usage of HttpCore APIs.
* It is NOT intended to demonstrate the most efficient way of building an HTTP client.
*
*
*
*/
public class search extends Activity {
public static void main(String[] args) throws Exception {
HttpParams params = new SyncBasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpRequestInterceptor[] {
// Required protocol interceptors
new RequestContent(),
new RequestTargetHost(),
// Recommended protocol interceptors
new RequestConnControl(),
new RequestUserAgent(),
new RequestExpectContinue()});
HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
HttpContext context = new BasicHttpContext(null);
HttpHost host = new HttpHost("localhost", 80);
DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
try {
HttpEntity[] requestBodies = {
new StringEntity(
"This is the first test request", "UTF-8"),
new ByteArrayEntity(
"This is the second test request".getBytes("UTF-8")),
new InputStreamEntity(
new ByteArrayInputStream(
"This is the third test request (will be chunked)"
.getBytes("UTF-8")), -1)
};
for (int i = 0; i < requestBodies.length; i++) {
if (!conn.isOpen()) {
Socket socket = new Socket(host.getHostName(), host.getPort());
conn.bind(socket, params);
}
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
"/android.php");
request.setEntity(requestBodies[i]);
System.out.println(">> Request URI: " + request.getRequestLine().getUri());
request.setParams(params);
httpexecutor.preProcess(request, httpproc, context);
HttpResponse response = httpexecutor.execute(request, conn, context);
response.setParams(params);
httpexecutor.postProcess(response, httpproc, context);
System.out.println("<< Response: " + response.getStatusLine());
System.out.println(EntityUtils.toString(response.getEntity()));
System.out.println("==============");
if (!connStrategy.keepAlive(response, context)) {
conn.close();
} else {
System.out.println("Connection kept alive...");
}
}
} finally {
conn.close();
}
}
}
我在android.jar中看不到这些类
是否有一个更简单的示例,可以针对web服务器实现一个事务,以获取用于android java应用程序的namevaluepair(JSON)?这些错误是因为您引用的类不是标准android发行版的一部分:请参阅和
Android重新打包Apache HTTP库的特定版本。如果您想使用这些类,您需要包含这些jar。如何包含这些jar?如果我包含非标准的android jar,我会在android设备上运行时遇到问题吗?我不知道您需要包含哪些特定的Apache jar:我假设您使用的是以前的代码,所以无论从哪里获得代码,都应该指向正确的jar文件。只需将它们添加到eclipse项目中,您就可以开始了。你的设备应该没有问题。好的,我现在就把它整理好了。我已经将这些额外的包作为构建路径的一部分包含在内,不再出现这些错误
The import org.apache.http.params.SyncBasicHttpParams cannot be resolved
The import org.apache.http.protocol.ImmutableHttpProcessor cannot be resolved