gpt4 book ai didi

java - 从 Android 调用 Asmx

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:51 26 4
gpt4 key购买 nike

我正在尝试从 Android 应用程序调用 Asmx(.net Web 服务)。但是当我提供数据时出现错误:

AndroidRuntime(591): FATAL EXCEPTION: Thread-75
AndroidRuntime(591): java.lang.NullPointerException: println needs a message

在显示数据的 logcat 中:

D\Req value1(540): NetPositionReport{arg0=64396; b=Om$@!#@M^#R; }

可能是什么问题?

MainActivity.java

package com.example.clientnetpositionreport;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
EditText e1;
Button b1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

b1=(Button) findViewById(R.id.btn);
b1.setOnClickListener(new OnClickListener(){
public void onClick(View v){

e1=(EditText)findViewById(R.id.evofclientcode);

Intent i=new Intent(MainActivity.this,GetReport.class);
i.putExtra("ClientCode",e1.getText().toString());
startActivity(i);
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

GetReport.java

package com.example.clientnetpositionreport;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;



import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class GetReport extends Activity{
private static final String NAMESPACE ="http://xxxxxxx
private static final String URL = "http://xxxxxxxxxxxx/services/xxxx.asmx";
private static final String METHOD_NAME = "xxxxxxx";
private static final String SOAP_ACTION = "http://xxxx/xxxxxxxxx";
private String webResponse = "";
private Handler handler = new Handler();
private Thread thread;

private TextView textView1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(1);
setContentView(R.layout.activity_tv);
textView1 = (TextView) findViewById(R.id.textview1);

//int val=getIntent().getExtras().getInt("Title");
String title= getIntent().getExtras().getString("ClientCode");
//Log.d("Data is", val);

System.out.println("Data is "+title);

startWebAccess(title);

}
public void startWebAccess(String a){
final String aa=a;
thread = new Thread(){
public void run(){
try{
Log.d("Req value0R", "Starting...");//log.d is used for debug
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//simple object access protocol
PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("arg0");
fromProp.setValue(aa);
fromProp.setType(String.class);
request.addProperty(fromProp);

PropertyInfo fromProp2 =new PropertyInfo();
fromProp2.setName("b");
fromProp2.setValue("Om$@!#@M^#R");
fromProp2.setType(String.class);
request.addProperty(fromProp2);

Log.d("Req value1", request.toString());

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = false;
envelope.setOutputSoapObject(request);

Log.d("Req value2", envelope.toString());
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
System.out.println("problem1");
androidHttpTransport.debug = true;
System.out.println("problem2");
Log.d("Req value2B", androidHttpTransport.toString());
System.out.println("problem3");
androidHttpTransport.call(SOAP_ACTION, envelope);
System.out.println("problem4");
Log.d("Req value2C", androidHttpTransport.toString());
System.out.println("problem5");
Object objectResult = (Object)envelope.getResponse();
System.out.println("problem6");
webResponse = objectResult.toString();
System.out.println("problem7");

}

catch(Exception e){
System.out.println("problem8");
Log.d("Req value4", e.getMessage() );
webResponse = "Connection/Internet problem";
webResponse = "Connection/Internet problem";
// Toast.makeText(getApplicationContext(), "Loading problem/Server down", Toast.LENGTH_SHORT).show();
}

handler.post(createUI);
}
};

thread.start();
}


final Runnable createUI = new Runnable() {

public void run(){
if(webResponse!=null) {

textView1.setText(webResponse);
}
else {
webResponse ="No data provided presently";
textView1.setText(webResponse);
}
}
};

}

note: The console output is till problem6. That means the value is Null (What I think.).So please keep that in mind.

Activity_Main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<TextView
android:id="@+id/tvofclientcode"
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Client Code"
/>
<EditText
android:id="@+id/evofclientcode"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_toRightOf="@+id/tvofclientcode"
android:inputType="text"/>
<Button
android:id="@+id/btn"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_below="@+id/tvofclientcode"
android:text="Submit"/>
</RelativeLayout>

可能是什么问题?

注意:log.d 方法正在获取字符串值,因此这不是某些相关案例中的问题。

最佳答案

看完你的代码我发现了很多小错误..

我不能在这里写出所有错误,所以只需将您的代码与我的代码进行比较并了解您的错误。

This is your Asmx Web-Service.

这里你必须传递两个输入参数

 <ClientCode>string</ClientCode>
<key>string</key>

但是你把所有的东西都传错了..

您的错误代码

PropertyInfo fromProp =new PropertyInfo();
fromProp.setName("arg0");
fromProp.setValue(aa);
fromProp.setType(String.class);
request.addProperty(fromProp);

PropertyInfo fromProp2 =new PropertyInfo();
fromProp2.setName("b");
fromProp2.setValue("Om$@!#@M^#R");
fromProp2.setType(String.class);
request.addProperty(fromProp2);

你必须这样通过

SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// simple object access protocol
request.addProperty("ClientCode", aa);
request.addProperty("key", "Om$@!#@M^#R");

GetReport.Class 的工作代码

package com.example.clientnetpositionreport;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

public class GetReport extends Activity {
private static final String NAMESPACE = "http://tempuri.org/"; // com.service.ServiceImpl
private static final String URL = "http://commodities.karvy.com/services/NetPositionReport.asmx";
private static final String METHOD_NAME = "NetPositionReport";
private static final String SOAP_ACTION = "http://tempuri.org/NetPositionReport";
private String webResponse = "";
private Handler handler = new Handler();
private Thread thread;

private TextView textView1;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(1);
setContentView(R.layout.fragment_item_detail);
textView1 = (TextView) findViewById(R.id.item_detail);

String title= getIntent().getExtras().getString("ClientCode");

System.out.println("Data is " + title);

startWebAccess(title);

}

public void startWebAccess(String a) {
final String aa = a;
thread = new Thread() {
public void run() {
try {
Log.d("Req value0R", "Starting...");
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
// simple object access protocol
request.addProperty("ClientCode", aa);
request.addProperty("key", "Om$@!#@M^#R");

Log.d("Req value1", request.toString());

HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
envelope.dotNet = true;
envelope.bodyOut = request;
// envelope.dotNet = true;
envelope.setOutputSoapObject(request);

Log.d("Req value2", envelope.toString());

androidHttpTransport.debug = true;
androidHttpTransport.call(SOAP_ACTION, envelope);

SoapObject resultData = (SoapObject) envelope.getResponse();
if (resultData != null)
webResponse = resultData.toString();
else
webResponse = "No Data found.";
System.out.println("webResponse : " + webResponse);

}

catch (Exception e) {
e.printStackTrace();
webResponse = "Connection/Internet problem";
}
handler.post(createUI);
}
};
thread.start();
}

final Runnable createUI = new Runnable() {

public void run() {
if (webResponse != null) {
textView1.setText(webResponse);
} else {
webResponse = "No data provided presently";
textView1.setText(webResponse);
}
}
};

}

这是代码,但我没有得到响应,可能是我传递了错误的 ClientCodekey

关于java - 从 Android 调用 Asmx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21979805/

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