在android中通过Http Post请求发布Json

时间:2014-06-25 08:25:51

标签: android json http-post

我需要在微粒Url中发布一个Json值我已经从IOS发送了这个值它工作正常但是当我试图通过android发布它时它给了我错误

Connection to http://192.168.1.151:8080 refused

final String jsonValue = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:auth=\"http://auth.mobility.kra.kuali.polus.com/\">" +
            "<soapenv:Header/>" +
            "<soapenv:Body>" +
            "<auth:authenticate>" +
            "<!--Optional:-->" +
            "<principalName>jenlu</principalName>" +
            "</auth:authenticate>" +
            "</soapenv:Body>" +
            "</soapenv:Envelope>";

    HttpPost httpPost = new HttpPost("http://192.168.1.151:8080/kc-devl/services/AuthService?wsdl=authService.wsdl");
    String response_string = null;

    try
    {
        StringEntity se = new StringEntity( jsonValue, HTTP.UTF_8 );

        httpPost.setHeader("Host","http://192.168.1.151:8080");
        httpPost.setHeader("Content-Type","application/json; charset=UTF-8");
        httpPost.setHeader("SOAPAction","");
        httpPost.setHeader("Content-Length",String.valueOf(jsonValue.length()));

        httpPost.setEntity(se);
        HttpClient client     = new DefaultHttpClient();
        HttpResponse response = client.execute(httpPost);
        response_string       = EntityUtils.toString(response.getEntity());
        Log.d("request", response_string);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

得到我的答案

public class SoapHTTPPostActivity extends Activity {

private String URL = "http://192.168.1.151:8080/kc-devl/services/AuthService?wsdl=authService.wsdl";
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    new getWebService().execute();

}

public class getWebService extends AsyncTask<String, Void, Void> {

    @Override
    protected Void doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String xml = null;
        try {
            xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
                    "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:auth=\"http://auth.mobility.kra.kuali.polus.com/\">"+
                    "<soapenv:Header/>"+
                    "<soapenv:Body>"+
                    "<auth:authenticate>"+
                    "<!--Optional:-->"+
                    "<principalName>shields</principalName>"+
                    "</auth:authenticate>"+
                    "</soapenv:Body>"+
                    "</soapenv:Envelope>";
        } catch (Exception e) {
            e.printStackTrace();
        }
        String request = String.format(xml, "12");
        HTTPOST httpost = new HTTPOST();
        httpost.getResponseByXML(URL, request);

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}

}     在这里输入代码

0 个答案:

没有答案