gpt4 book ai didi

Android+JSON解析使用SOAP webservices

转载 作者:行者123 更新时间:2023-11-29 00:47:58 24 4
gpt4 key购买 nike

我有一个关于如何在 Android 中使用 JSON 集成的问题。例如,我有一个登录页面,我想使用 SOAP web 服务对其进行身份验证以对其进行导航。我想使用 JSON 集成,我该怎么做?有谁能够帮助我?如果您有示例,请提供。

这是我在 logcat 中显示输出的代码。现在如何在模拟器上检索?

package com.temp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.kxml2.kdom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;


import android.app.Activity;
import android.os.Bundle;
import android.provider.Browser;
import android.text.StaticLayout;
import android.util.Log;
import android.widget.TextView;



public class temp extends Activity{
private static final String NAMESPACE = "http://tempuri.org/";

private static final String URL ="http://";

private static final String SOAP_ACTION = "http://tempuri.org/Login";

private static final String METHOD_NAME = "Login";
TextView tv;
String string;
@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.main);
string =
"{\"Geninfo\": " +
"{\"appname\":\"\"," +
"\"appver\":\"1.0.0\"," +
"\"deviceType\":\"\"," +
"\"deviceOSVersion\":\"3.0\"," +
"\"phoneDeviceID\":\"0\"}," +
"\"Login\":" +
"{\"emailID\":\"\",abc@gmailcom"+
"\"Password\":\"123456\"}}";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false); // Comment: 1

HttpPost httppost = new HttpPost(URL); // Comment: 2

// connection.setDoOutput(true);

URL u;
URLConnection uc;
try {
u = new URL(URL);
uc = u.openConnection();


HttpURLConnection connection = (HttpURLConnection) uc;
StringBuffer soap = new StringBuffer();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Host","191.118.0.4");
connection.setRequestProperty("Content-Type","application/soap+xml;charset=utf-8");
connection.setRequestProperty("Content-Length",String.valueOf(soap.length()));

InputStream is = connection.getInputStream();

//HttpPost httpget = new HttpPost(URL);
connection.setRequestMethod(soap.toString());
//DefaultHttpClient client = new DefaultHttpClient();
// httppost.setHeader("POST","/makenewbuddies/mobileapp/APImobilelogin.asmx HTTP/1.1");
// httppost.setHeader("SOAPAction", SOAP_ACTION);
// httppost.setHeader("Content-Type", "application/soap+xml; charset=utf-8");
// Comment: 4
soap.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
soap.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">") ;
soap.append("<soap:Body>");
soap.append("<LoginRequest xmlns=\"http://tempuri.org/\">");
soap.append("<JSONRequestString>"+string+"</JSONRequestString>");

//soap.append("<strPassword>"+password+"</strPassword>");
// soap.append("<strXml_In>");
// soap.append("&lt;XML-GetDetailsOfAMobileArticle-IN DATACLASS= 'DetailsOfAMobileArticle'&gt; &lt;CONDITION ARTICLE_ID= '"+articleid+"' CALLER='' USER_ID='"+userid+"'/&gt; &lt;/XML-GetDetailsOfAMobileArticle-IN&gt;");
// soap.append("</strXml_In>");
soap.append("</LoginRequest>");
soap.append("</soap:Body>");
soap.append("</soap:Envelope>");
Log.d("soap value1 is",soap.toString());

// String len=String.valueOf(soap.length());
// httppost.setHeader("Content-Length", String.valueOf(soap.length()) );
try {
// StringEntity strent= new StringEntity(soap.toString());
// strent.setContentType("application/soap+xml; charset=utf-8");

// httppost.setEntity(strent);
ResponseHandler<String> response = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost,response);


Log.v("soap response is",responseBody);

// System.out.println("Thanks God" + responseBody);
} catch(Exception e) {
e.printStackTrace();
System.out.println("Exception Thanks God : " + e.toString());

}
OutputStream out = connection.getOutputStream();

Writer wout = new OutputStreamWriter(out);
wout.write(soap.toString());

wout.flush();

wout.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

//InputStream in = connection.getInputStream();

String result;
//int c;
while ((result=rd.readLine()) != null) {

System.out.println(result);

} }catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}



catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
HttpClient client = new DefaultHttpClient();

HttpGet get = new HttpGet(URL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
//do something with the response
Log.i("GET RESPONSE",EntityUtils.toString(resEntityGet));
}
} catch (Exception e) {
e.printStackTrace();
}
}



}

最佳答案

Android SDK 包括 JSON 实现。您提交您的请求,其中正文格式为 JSON。如果该请求被接受,您的服务将返回响应,其中响应正文将采用 JSON 格式。之后,您只需按如下方式声明 JSON 对象即可进行解析:

JSONObject json = new JSONObject(rawJson);

在这里查看更多信息 http://goo.gl/iVXI4或者只是谷歌

关于Android+JSON解析使用SOAP webservices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5228547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com