Java 如何从Android向web服务发布参数?
我可以使用HTTPPOST和response检索xml文件。但是,现在我需要发布一个Java 如何从Android向web服务发布参数?,java,android,xml,web-services,Java,Android,Xml,Web Services,我可以使用HTTPPOST和response检索xml文件。但是,现在我需要发布一个String参数以及URL。下面的代码告诉我HTTP staus代码不正常(即500),因此它返回null,然后我得到NullPointerException package com.JobsWebService; import java.io.IOException; import java.io.Reader; import java.io.StringReader; import java.io.Unsup
String
参数以及URL。下面的代码告诉我HTTP staus代码不正常(即500),因此它返回null
,然后我得到NullPointerException
package com.JobsWebService;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
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.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
import android.util.Log;
public class XmlConnection {
private static final String url = "http://www.accuservlite.com/AccumobileWS/TestService.asmx/RyanMB_GetJobs";
private DefaultHttpClient client = new DefaultHttpClient();
String param= "Johnny";
public List<NewJob> RyanMB_GetJobs() {
try {
String xmlData = retrieve(url);
Serializer serializer = new Persister();
Reader reader = new StringReader(xmlData);
ArrayOfNewJob testService = serializer.read(ArrayOfNewJob.class,
reader, false);
Log.i("gary", "Worked");
return testService.NewJob;
} catch (Exception e) {
Log.i("gary", e.toString());
}
return null;
}
// method retrieve
public String retrieve(String url) throws UnsupportedEncodingException {
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("Johnny", "Johhny"));
UrlEncodedFormEntity postEntity = new UrlEncodedFormEntity(qparams,
HTTP.UTF_8);
HttpPost getRequest = new HttpPost(url);
getRequest.setEntity(postEntity);
try {
HttpResponse getResponse = client.execute(getRequest);
final int statusCode = getResponse.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
HttpEntity getResponseEntity = getResponse.getEntity();
if (getResponseEntity != null) {
return EntityUtils.toString(getResponseEntity);
}
} catch (IOException e) {
Log.i("gary", "Error for URL " + url, e);
getRequest.abort();
}
return null;
}
package com.jobservice;
导入java.io.IOException;
导入java.io.Reader;
导入java.io.StringReader;
导入java.io.UnsupportedEncodingException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.HttpStatus;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpGet;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.apache.http.protocol.http;
导入org.apache.http.util.EntityUtils;
导入org.simpleframework.xml.Serializer;
导入org.simpleframework.xml.core.Persister;
导入android.util.Log;
公共类XmlConnection{
私有静态最终字符串url=”http://www.accuservlite.com/AccumobileWS/TestService.asmx/RyanMB_GetJobs";
私有DefaultHttpClient=新的DefaultHttpClient();
字符串param=“Johnny”;
公共列表RyanMB_GetJobs(){
试一试{
字符串xmlData=retrieve(url);
Serializer Serializer=new Persister();
读卡器=新的StringReader(xmlData);
ArrayOfNewJob testService=serializer.read(ArrayOfNewJob.class,
读卡器(错误);
Log.i(“gary”,“Worked”);
返回testService.NewJob;
}捕获(例外e){
Log.i(“gary”,e.toString());
}
返回null;
}
//方法检索
公共字符串检索(字符串url)引发UnsupportedEncodingException{
List qparams=new ArrayList();
添加(新的BasicNameValuePair(“约翰尼”、“约翰尼”);
UrlEncodedFormEntity postEntity=新的UrlEncodedFormEntity(qparams,
HTTP.UTF_8);
HttpPost getRequest=新的HttpPost(url);
getRequest.setEntity(postEntity);
试一试{
HttpResponse getResponse=client.execute(getRequest);
final int statusCode=getResponse.getStatusLine().getStatusCode();
if(statusCode!=HttpStatus.SC\u OK){
返回null;
}
HttpEntity getResponseEntity=getResponse.getEntity();
if(getResponseEntity!=null){
返回EntityUtils.toString(getResponseEntity);
}
}捕获(IOE异常){
Log.i(“gary”,“URL错误”+URL,e);
getRequest.abort();
}
返回null;
}
}500是一个服务器错误,我觉得这段代码没有什么明显的错误。您是否针对post请求测试了服务器?当您为服务器提供名称-值对时,服务器是否需要字符串
您可以尝试使用进行测试。要在您的HttpPost对象中添加POST参数,您将使用NameValuPair列表:
List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("name_param1", "value_param1"));
另外,还有另一个有用的工具,用于生成对服务器的HTTP GET/POST/PUT/etc请求,我使用它。是的。它是用java编写的,与平台无关。
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(formEntity);