Android如何访问soap web服务并使用json解析数据
我有一段关于使用soap web服务和json解析进行登录身份验证的代码,但我不清楚它是否正确,请指导我?? 如果可能,请提供我的代码 这是我的密码:-Android如何访问soap web服务并使用json解析数据,android,json,soap,Android,Json,Soap,我有一段关于使用soap web服务和json解析进行登录身份验证的代码,但我不清楚它是否正确,请指导我?? 如果可能,请提供我的代码 这是我的密码:- package com.devstream.http; import java.io.*; import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapPrimitive; impo
package com.devstream.http;
import java.io.*;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
ImageButton b;
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL ="http:....asmx";
private static final String SOAP_ACTION = "http://tempuri.org/LoginRequest";
private static final String METHOD_NAME = "LoginRequest";
protected static final String END_DOCUMENT =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
b=(ImageButton)findViewById(R.id.ImageButton01);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
String sUserName,sPassword;
EditText usernameEditText = (EditText)findViewById(R.id.EditText01);
EditText passwordEditText = (EditText)findViewById(R.id.EditText02);
if(usernameEditText == null ||passwordEditText == null){
Toast.makeText(getApplicationContext(), "Please Enter Valid Email and Password", Toast.LENGTH_LONG).show();
//Log.i("enter Correct details", null);
}else if(usernameEditText != null || passwordEditText != null){
sUserName = usernameEditText.getText().toString();
sPassword = passwordEditText.getText().toString();
String string =
"{\"Geninfo\": " +
"{\"appname\":\"MNB\"," +
"\"appver\":\"1.0.0\"," +
"\"deviceType\":\"BlackBerry\"," +
"\"deviceOSVersion\":\"3.0\":" +
"\"phoneDeviceID\":\"9150107470\"}," +
"\"Login\":" +
"{\"emailID\": " + sUserName +
"\"Password\": "+ sPassword + "}}";
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty("JsonRequestString",string);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.encodingStyle = SoapSerializationEnvelope.ENC2001;
envelope.dotNet=true;
envelope.setOutputSoapObject(request);
Log.i("Given Input",string);
Log.i("LoginDetail","Username : " +sUserName+ "Password :" +sPassword);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.debug=true;
System.out.println("request: " + androidHttpTransport.requestDump);
System.out.println("response:"+androidHttpTransport.responseDump);
androidHttpTransport.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapPrimitive resultstring = (SoapPrimitive) envelope.getResponse();
Log.i("OUTPUT",resultstring.toString()); }
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
next();
}}//end else
});
XmlPullParserFactory factory;
try {
factory = XmlPullParserFactory.newInstance();
XmlPullParser xpp = factory.newPullParser();
String n=xpp.getText().toString();
while( n != END_DOCUMENT ) {
try {
// do your parsing element-by-element
} catch( Throwable e ) {
Log.w("error while parsing file: " + e.getMessage(),"");
// you may want to catch any other exceptions and do something about them
}
}
} catch (XmlPullParserException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void setUserNameText(String $emailid){
EditText usernameEditText = (EditText)findViewById(R.id.EditText01);
usernameEditText.setText($emailid);
}
public void setPasswordText(String $password){
EditText passwordEditText = (EditText)findViewById(R.id.EditText02);
passwordEditText.setText($password);
}
private void next(){
Intent i=new Intent(getApplicationContext(), Home.class);
startActivity(i);
}
}
package com.devstream.http;
导入java.io.*;
导入org.ksoap2.SoapEnvelope;
导入org.ksoap2.serialization.SoapObject;
导入org.ksoap2.serialization.SoapPrimitive;
导入org.ksoap2.serialization.SoapSerializationEnvelope;
导入org.ksoap2.transport.HttpTransportSE;
导入org.xmlpull.v1.XmlPullParser;
导入org.xmlpull.v1.XmlPullParserException;
导入org.xmlpull.v1.XmlPullParserFactory;
导入android.app.Activity;
导入android.content.Intent;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.EditText;
导入android.widget.ImageButton;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
图像按钮b;
私有静态最终字符串命名空间=”http://tempuri.org/";
私有静态最终字符串URL=“http:..asmx”;
私有静态最终字符串SOAP_ACTION=”http://tempuri.org/LoginRequest";
私有静态最终字符串方法\u NAME=“LoginRequest”;
受保护的静态最终字符串END_DOCUMENT=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
b=(ImageButton)findViewById(R.id.ImageButton01);
b、 setOnClickListener(新的OnClickListener()
{
公共void onClick(视图v){
字符串sUserName,sPassword;
EditText用户名EditText=(EditText)findViewById(R.id.EditText01);
EditText密码EditText=(EditText)findViewById(R.id.EditText02);
如果(usernameEditText==null | | passwordEditText==null){
Toast.makeText(getApplicationContext(),“请输入有效的电子邮件和密码”,Toast.LENGTH\u LONG.show();
//Log.i(“输入正确的详细信息”,空);
}else if(usernameEditText!=null | | passwordEditText!=null){
sUserName=usernameEditText.getText().toString();
sPassword=passwordEditText.getText().toString();
字符串=
“{\'Geninfo\”:”+
“{\'appname\':\'MNB\',”+
“\'appver\':\'1.0.0\',”+
“\“deviceType\”:\“BlackBerry\”,”+
“\“deviceOSVersion\”:\“3.0\”:”+
“\“phoneDeviceID\”:\“9150107470\”+
“\“登录\”:”+
“{\“emailID\”:“+sUserName”+
“\”密码\“:“+sPassword+”}}”;
SoapObject请求=新的SoapObject(名称空间、方法名称);
addProperty(“JsonRequestString”,string);
SoapSerializationEnvelope=新的SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.encodingStyle=SoapSerializationEnvelope.ENC2001;
envelope.dotNet=true;
envelope.setOutputSoapObject(请求);
Log.i(“给定输入”,字符串);
Log.i(“LoginDetail”,“用户名:+sUserName+”密码:+sPassword);
HttpTransportSE androidHttpTransport=新的HttpTransportSE(URL);
androidHttpTransport.debug=true;
System.out.println(“请求:”+androidHttpTransport.requestDump);
System.out.println(“响应:”+androidHttpTransport.responseDump);
androidHttpTransport.setXmlVersionTag(“”);
试一试{
调用(SOAP_操作,信封);
SoapPrimitive结果字符串=(SoapPrimitive)信封。getResponse();
Log.i(“输出”,resultstring.toString());}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
next();
}}//结束其他
});
XmlPullParserFactory工厂;
试一试{
factory=XmlPullParserFactory.newInstance();
XmlPullParser xpp=factory.newPullParser();
字符串n=xpp.getText().toString();
while(n!=结束文档){
试一试{
//逐个元素进行分析
}捕获(可丢弃的e){
w(“分析文件时出错:+e.getMessage(),”);
//您可能希望捕获任何其他异常并对其采取措施
}
}
}捕获(XmlPullParserException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
public void setUserNameText(字符串$emailid){
EditText用户名EditText=(EditText)findViewById(R.id.EditText01);
usernameEditText.setText($emailid);
}
public void setPasswordText(字符串$password){
EditText密码EditText=(EditText)findViewById(R.id.EditText02);
passwordEditText.setText($password);
}
私有void next(){
Intent i=新Intent(getApplicationContext(),Home.class);
星触觉(i);
}
}
在logcat中运行此程序时,我可以看到输入、用户名和密码,但它显示请求和响应为空。我无法理解问题所在,请指导我???请按照以下代码操作。在这里,我在服务器上发送请求,获取响应并用json解析该响应
public void Login(final String UserName,final String Password)
{
try
{
new Thread()
{
public void run()
{
try
{
HttpPost postMethod = new HttpPost("Your Url");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("UserName","Your UserName");
nameValuePairs.add(new BasicNameValuePair("Password", "Your Password"));
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
DefaultHttpClient hc = new DefaultHttpClient();
HttpResponse response = hc.execute(postMethod);
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null)
{
InputStream inStream = entity.getContent();
result= convertStreamToString(inStream);
jsonObject = new JSONObject(result);
responseHandler.sendEmptyMessage(0);
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
}.start();
responseHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
try
{
Log.i("-------------------- Response",result);
if(jsonObject.getJSONObject("response").getString("msg").equalsIgnoreCase("Success"))
{
Log.i("Login","--- Login Success");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
}
catch(Exception e)
{
}
}
}你能帮我吗
public static String convertStreamToString(InputStream is)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try
{
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return sb.toString();