如何张贴一个参数从Android Web服务? [英] How do I post a parameter to a web service from Android?

查看:268
本文介绍了如何张贴一个参数从Android Web服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用HTTP POST和响应检索XML文件。不过,现在我需要发布字符串参数以及网址。下面code告诉我,HTTP居留制code是不正常(即500),等它返回,然后我得到一个 NullPointerException异常

I can retrieve an xml file using HTTP post and response. However, now I need to post a String parameter as well as the URL. The following code is telling me that the HTTP staus code is not ok (i.e., 500) and so it returns null and then I get a 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;
}

}

推荐答案

要添加POST参数YOUT HttpPost对象,您将使用NameValuPair的列表:

To add POST parameters in yout HttpPost object, you will use a List of NameValuPair :

List<NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("name_param1", "value_param1"));

和这个参数添加到您的HttpPost:

And to add this parameter to your HttpPost :

UrlEncodedFormEntity formEntity  = new UrlEncodedFormEntity(parameters);
httpPost.setEntity(formEntity);

这篇关于如何张贴一个参数从Android Web服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆